Skip to content

Instantly share code, notes, and snippets.

@pjb3
Created January 7, 2014 04:53
Show Gist options
  • Save pjb3/8294786 to your computer and use it in GitHub Desktop.
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.
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