Created
June 4, 2011 11:33
-
-
Save ryanlecompte/1007826 to your computer and use it in GitHub Desktop.
Superfluous use of instance_exec
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
module M | |
def m1 | |
puts "hi from M" | |
end | |
def handle(&block) | |
block.call | |
end | |
def handle2(&block) | |
# No need for instance_exec here since the block | |
# that is passed in recorded 'self' to be the | |
# class that extended M, i.e. "C" | |
instance_exec(&block) | |
end | |
end | |
class C | |
extend M | |
# Blocks are closures and capture value of 'self' here, | |
# which in this case is C. C extends M, so M's instance | |
# methods become available to C as "class methods" | |
handle { m1 } | |
handle2 { m1 } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment