Skip to content

Instantly share code, notes, and snippets.

@ryanlecompte
Created June 4, 2011 11:33
Show Gist options
  • Save ryanlecompte/1007826 to your computer and use it in GitHub Desktop.
Save ryanlecompte/1007826 to your computer and use it in GitHub Desktop.
Superfluous use of instance_exec
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