Post by Peter Cooper on August 4th, 2006
Dynamically adding methods to classes through their objects

- Applying conditions to attribute setters
- How to create a Ruby extension in C in under 5 minutes
- O’Reilly releases “Ruby Cookbook” : 906 pages of Ruby goodness


Lucas Carlson comes up with a cute trick to make Ruby feel a little more like a prototyped language by allowing you to define methods on a class in real-time through child objects, like so:
f = Foo.new f.greet = lambda {|t| "Hello #{t}!"} f.greet "Lucas Carlson" # => Hello Lucas Carlson! j = Foo.new j.greet "World" # => Hello World!
Find out how. A cute trick.

Click here to add on del.icio.us









August 4th, 2006 at 4:16 pm
require 'ostruct'
August 5th, 2006 at 7:45 am
Kent, if it works how I think it works(I havent checked yet), OpenStruct won't work.
f = OpenStruct.new
f.puts = lamba{|t|puts t}
f.puts(1) #=> gives you a proc