From: Robert Dober Date: 2008-01-19T20:51:26+09:00 Subject: Re: passing method references in python & ruby On Jan 18, 2008 6:50 PM, rpardee@gmail.com wrote: > My momentary python envy (hmmm...) has now passed. ;-) Well that is good, but for the wrong reason :( Our Rubyists where cheating on you as you had a first class object called a function, and we were just using names. Now there are no functions in Ruby so we cannot pass them on, we do however have proc objects which would mimick what you have done in Python firstway =lambda{ |x| x+1} otherway = lambda{ |x| x - 1} def anyway away, anumber away.call anumber end However the interesting and somewhat frustrating stuff starts with methods which are first class objects in Ruby class A def first x; x+1 end def second x; x-1 end @f = instance_method :first @s = instance_method :second class << self attr_reader :f, :s end def any method, number method.bind( self ).call number end end p A.new.any(A.f, 41) p A.new.any(A.s,43) Things are however a little bit trickier when it comes to binding instance_methods look at this code class B def b; 42 end end p B.instance_method(:b).bind(Object.new).call # :((( I still fail to see the need for this restriction Now lots of trickery is needed if you want to do things like these module M def m; 42 end end p Object.new.send(:extend, M).m If you are interested in this stuff you might have a look at the implementation of Traits in Ruby http://rubyforge.org/projects/ruby-traits/ HTH Robert -- http://ruby-smalltalk.blogspot.com/ --- Whereof one cannot speak, thereof one must be silent. Ludwig Wittgenstein