* Create your FREE website now *

Late Binding and Blocks

Tags:  

 
# from John Lam's IronRuby presentations
# http://www.iunknown.com/2007/11/ironruby-presen.html
# the Microsoft ECS presentation

#slide 36
#shows late binding and dynamic scope in ruby...
class B
def foo; 'foo'; end
def use_block(&b); puts instance_eval(&b); end
end

# note that foo is not defined in this scope
# so it will bind to the foo method within B!
B.new.use_block { foo } #=> foo

# now define foo in this scope
# and it will use this foo, not the one in B.
foo = 'bar'
B.new.use_block { foo } #=> bar
  




 RSS of this page

Written by:   Written by:   Version:   Last Edited By:   Modified