Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Ruby Crush

itchy
March 07, 2012

Ruby Crush

A Metaprogramming Primer

itchy

March 07, 2012
Tweet

More Decks by itchy

Other Decks in Programming

Transcript

  1. Definition Code that writes code Like erb? Code that modifies

    code at runtime Wednesday, March 7, 2012
  2. Quick Examples Decide how to respond to a method call

    at runtime Wednesday, March 7, 2012
  3. Quick Examples Ghost Methods (Profile.deligates User) Decide how to respond

    to a method call at runtime Wednesday, March 7, 2012
  4. Object Model Because, Metaprogramming is code that modifies code at

    runtime We need to know about the Ruby Object Model before we go too far. Wednesday, March 7, 2012
  5. Object Model Whataburger Data Structure in Detail http:/ /www.atalon.cz/ rb-om/ruby-object-

    model/ http:/ / www.youtube.com/ watch?v=UyAoUX6GYI8 Wednesday, March 7, 2012
  6. Object Model Whataburger Data Structure in Detail Movie http:/ /www.atalon.cz/

    rb-om/ruby-object- model/ http:/ / www.youtube.com/ watch?v=UyAoUX6GYI8 Wednesday, March 7, 2012
  7. Object Model Whataburger Data Structure in Detail Movie My take

    on it http:/ /www.atalon.cz/ rb-om/ruby-object- model/ http:/ / www.youtube.com/ watch?v=UyAoUX6GYI8 Wednesday, March 7, 2012
  8. pseudo_inheritance Problem: How do we overcome different paradigms between OO

    and RDBMS to allow ActiveRecord (AR) objects to inherit from AR objects? Both systems have objects with Behavior, Identity & State. OO objects that inherit from each other should have direct access to all superclasses’ behavior identity and state. RDBMS entities relate to each other. RDBMS entries should normalize state across multiple tables. Because AR stores state in RDBMS, objects that should inherit from other AR objects are required to use composition. Can we use the tools presented in this talk to overcome these limitations? Wednesday, March 7, 2012
  9. pseudo_inheritance Example: Our system has users, profiles, hosts, presenters and

    topics. A user is anyone with access to the system, each user has a profile that describes the user, hosts are users who host a topic, presenters are users who present a topic and topics are well, topics. Users can host and present multiple topics. Wednesday, March 7, 2012
  10. pseudo_inheritance Objects class User < ARB has_one :profile class Profile

    < ARB belongs_to :user has_many :hosts has_many :presenters class Host < ARB belongs_to :profile class Presenter < ARB belongs_to :profile class Topic < ARB has_one :host has_one :presenter Tables users ------------------- profiles profiles ------------< hosts profiles ------------< presenters topics ---------------< hosts ---------------< presenters So we have: To get a Topic’s presenter’s email: @topic.presenter.profile.user.email Wednesday, March 7, 2012
  11. pseudo_inheritance Objects class User < ARB class Profile < User

    class Host < Profile class Presenter < Profile class Topic < ARB has_one :host has_one :presenter Tables users ------------------- profiles profiles ------------< hosts profiles ------------< presenters topics ---------------< hosts ---------------< presenters We should have: To get a Topic’s presenter’s email: @topic.presenter.email Wednesday, March 7, 2012
  12. Don’t Forget to Remember Yukirhiro Matsumoto: ... computers don't mind

    if I must make effort to communicate with them or if it is easy to communicate with them... . We humans care about the effort we pay. Often people, especially computer engineers, focus on the machines. They think, "By doing this, the machine will run faster. By doing this, the machine will run more effectively. By doing this, the machine will something something something." They are focusing on machines. But in fact we need to focus on humans, on how humans care about doing programming or operating the application of the machines. Wednesday, March 7, 2012
  13. Bibliography Venners, Bill. The Philosophy of Ruby: A conversation with

    Yukihiro Matsumoto, Part I. [Article] (September 29, 2003). Artima Developer, From http://www.artimadeveloper.com/intv/ruby4.html Thomas, Dave, Andy Hunt and Chad Fowler. Programming Ruby 1.9: The Pragmatic Programmers’ Guide. Dallas, TX: The Pragmatic Bookshelf, 2011 Perrotta, Paolo. Metaprogramming Ruby: Program Like the Ruby Pros. Dallas, TX: The Pragmatic Bookshelf, 2011 Thomas, Dave. The Ruby Object Model and Metaprogramming. Screen casts read by the author. Dallas, TX: The Pragmatic Bookshelf, 2011 Brady, David. Ruby Rouges: Episode 39, Programming Language Fundamentals. [Podcast], January 28, 2012 Pavalta, Ondrej (March 2, 2011) The Ruby Object Model: Data Structure in detail. From http://www.atalon.cz/rb-om/ruby-object- model/ The Ruby Object Model as Interpreted by Susan Muldowney [Video]. (June 13, 2011) From http://www.youtube.com/watch? v=UyAoUX6GYI8 McSpadden, Mark. Ruby Object Model [Tweet]. (August 22, 2011) From http://www.twitter.com, @markmcspadden Dearing, Jesse. Less Magic [Tweet]. (January 4, 2012) From http://www.twitter.com, @jessedearing Wednesday, March 7, 2012