I assume you are doing this in the console, so you are actually defining a method on Object, which then defines a method for all the children Object... which is all. So your method:
def moo
puts "moo"
end
Returns nil, and since you defined a method on Object, NilClassalso has this method, you can call mooon NilClass.
If you do:
class Foo
def bar
1 + 1
end
end
And then:
f = Foo.new
f.bar.bar
You get:
NoMethodError: undefined method `bar' for 2:Fixnum
source
share