Skip to content

Instantly share code, notes, and snippets.

@niklas
Created May 25, 2011 15:16
Show Gist options
  • Save niklas/991148 to your computer and use it in GitHub Desktop.
Save niklas/991148 to your computer and use it in GitHub Desktop.
yielding to a block within class_eval does not set self in the block?
class A
def self.my_eval
B.class_eval do
puts "self in A.my_eval: #{self}"
yield
end
end
end
class B
A.my_eval do
puts "self in B: #{self}"
end
end
A.my_eval do
puts "self in top level block: #{self}"
end
# this will output:
# self in A.my_eval: B
# self in B: B
# self in A.my_eval: B
# self in top level block: main
# 2 and 4 are really strange.. I would have guessed self would be forwarded within the block.
# binding?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment