= Meta-Magic in Rails: Become a Master Magician by Nic Williams == Ruby is obviously special Was writing in .NET Noticed the RoR marketing machine, promising to write code fast "I quit being a coder because I hated it - turned out I just hated Java" == But why? Data in --> Process --> Data out Why do I need a specific language? == Ruby has === 1. Flexible syntax class Post attr_accessor(:title) end 5.times { |n| puts n } "I'm not the bloke to tell you what to do" === 2. Dynamic behaviour If you don't like a library, just change it Post.find_by_year_and_month(2007, 9) user = add_user do name "Dr Nic" country "Australia" end == Perl - Puppy "Perl is like a puppy - it's fun to use, but it doesn't know anything about itself" Fowler: "Oh yes it does" - Cute and fun to use - No understanding of itself - Wees on the floor == Java - Keith Richards - Not so cute anymore - Can tell stories about himself - Can't change his behaviour - (Should have been dead already) Java was fun back in the 90's when it first came out. I had to write some Java, and I installed Eclipse, and it was almost fun to use with a decent editor Keith is very good at talking about himself - but he has no notion about what he is doing, and that he can modidify it == Ruby - Neo (McGyver, A-Team) - Knwos about his environment - Knwos what he can do - Can adapt behaviours - Can get the ladies == Meta programming The language knows about itself and can describe itself - is just another library to use == Introspection (see behaviour) - What can the code learn about itself? - Class objects, Interfaces - Methods, Constructors === Java introspection - Java has it, but it requires a truck-load of code === Ruby introspection - is cleaner neo = Person.new neo.class # => Person neo.methods # => [.. 'activity', 'activity=' ...] neo.sen("activity=", "Stop bullets in mid-air") == Reflection (change behaviour) - See your world - Change your world The more important part of meta-programming === Java Relection API - Doesn't actually do anything, should have called the introspection API === Ruby class << Person defined_method :show_activity do puts self.activity end end Conceptually, code is data: {:code => "data"} Ruby is programmable programming language === method_missing Probably the most useful meta-programming tool Person.find_by_firstname_and_lastname("Nic", "Williams") rxml views / builder def method_missing(methid, *args, &block) ... super end Please document above what this method makes you able to do, because the code is unreadeable You could do: 5.to_post instead of Post.find(5) - Uses this for debugging === Create new classes class Person end Person = Class.new klass = Class.new Object.const_set "Person", klass # Could be any module def klass.me puts "moi" end class << klass end klass.class_eval do include MyMod end ==== const_missing Disappointing that Dave Thomas apologized about talking about Rails - Dr Nic's Magic Models A demonstration of what you can do with with meta-progamming - Allows for dynamic creaton of ActiveRecord classes == Hpricot(open(url)) How do you use the module as a method? Hpricot method declared before the class == eval eval <<-EOS ... EOS === class_eval / module_eval against the Class === instance_eval against an instance == Gem: guessmethod Prson.nw.nam RejeConf tonight at 21.00 http://www.rug-b.de/wiki/show/RejectConf