test = "a" test.class_eval do # what is going on here??? end
+2
A:
Well, on my computer, you get a NoMethodError because the String class doesn't have a class_eval
method :)
J Cooper
2010-08-17 05:22:59
+2
A:
I think that in vanilla Ruby, that is illegal. For instance, if you try it in irb, you'll get a NoMethodError.
In a rails console, there might be extra methods added in that cause strings to behave like classes.
Sam
2010-08-17 05:24:02
+2
A:
ActiveSupport adds class_eval
to Object so it can be used on anything, not just classes.
In your example, what it does is the equivalent of:
test = "a"
class << test
# do stuff
end
The actual implementation as of Rails 2.3 is here: http://github.com/rails/rails/blob/2-3-stable/activesupport/lib/active_support/core_ext/object/singleton_class.rb
Todd Yandell
2010-08-17 06:44:49