Skip to content

Instantly share code, notes, and snippets.

@mmower
Created September 1, 2008 10:14
Show Gist options
  • Save mmower/8290 to your computer and use it in GitHub Desktop.
Save mmower/8290 to your computer and use it in GitHub Desktop.
class WhyWeNeedProcs
def initialize
@multiplier = lambda { |n| n }
end
def multiply( m, &multiplier )
if block_given?
# Note the use of & in the method specification explicitly
# converts any block passed to the method into a proc assigned
# to the local variable "multiplier"
@multiplier = multiplier
end
@multiplier[m]
end
end
wwnp = WhyWeNeedProcs.new
puts wwnp.multiply( 5 )
puts wwnp.multiply( 5 ) { |n| 2*n }
puts wwnp.multiply( 10 )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment