Here is a little Ruby trivium for you.
Type this into IRB:
def foo
def bar
1
end
end
foo.bar
=> 1
Is this some magical lightweight object creation syntax so you can do cool method chaining? Let’s try another example:
def foo
def foo
1
end
end
foo
=> nil
foo.foo
=> 1
So far so good. But now, type:
foo
=> 1
WTF? Is this a defect in Ruby?? Post your responses in the comments.
(Warning: this is a trick question)
About the Author