Created
September 1, 2008 10:14
-
-
Save mmower/8290 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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