Created
March 22, 2011 06:49
-
-
Save satoryu/880868 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
def create_class | |
Class.new | |
end | |
MyClass = create_class | |
MyClass.class_eval do | |
p self #=> #<Class:0xb775f6a4> | |
def greet | |
puts 'hi' | |
end | |
end | |
obj = MyClass.new | |
obj.greet |
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
def create_class | |
cl = Class.new | |
cl.class_eval do | |
p self #=> #<Class:0xb775f6a4> | |
yield | |
end | |
return cl | |
end | |
p self #=> main | |
MyClass = create_class do | |
p self #=> main | |
def greet | |
puts 'hello' | |
end | |
end | |
obj = MyClass.new | |
obj.send(:greet) | |
# obj.greet #=> class_eval_problem2.rb:23: private method `greet' called for #<MyClass:0xb78b9644> (NoMethodError) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment