Created
January 7, 2014 04:53
-
-
Save pjb3/8294786 to your computer and use it in GitHub Desktop.
This is why people get class_eval and instance_eval confused in Ruby. You create class methods with instance eval and instance methods with class eval.
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 Foo | |
instance_eval <<-end_eval, __FILE__, __LINE__ | |
def bar | |
"class method" | |
end | |
end_eval | |
class_eval <<-end_eval, __FILE__, __LINE__ | |
def bar | |
"instance method" | |
end | |
end_eval | |
end | |
puts Foo.bar # class method | |
puts Foo.new.bar # instance method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment