Reputation: 20367
I happened to be working on a Singleton class in ruby and just remembered the way it works in factory_girl. They worked it out so you can use both the long way Factory.create(...)
and the short way Factory(...)
I thought about it and was curious to see how they made the class Factory
also behave like a method.
They simply used Factory
twice like so:
def Factory (args)
...
end
class Factory
...
end
My Question is: How does ruby accomplish this? and Is there danger in using this seemingly quirky pattern?
Upvotes: 2
Views: 741
Reputation: 369428
Methods and variables live in different namespaces. So, you can have both a method and a variable (or in this case a constant) with the same name. However, when using them, Ruby needs to be able to distinguish between them. In general, that's not a problem: messages have receivers, variables don't. Messages have arguments, variables don't. Variables are assigned to, messages aren't.
The only problem is when you have no receiver, no argument and no assignment. Then, Ruby cannot tell the difference between a receiverless message send without arguments and a variable. So, it has to make up some arbitrary rules, and those rules are basically:
Note that for a message send with arguments (even if the argument list is empty), there is no ambiguity.
test()
: obviously a message send, no ambiguity heretest
: might be a message send or a variable; resolution rules say it is a message send (unless there has been an assignment to test
before)Test()
: obviously a message send, no ambiguity hereself.Test
: also obviously a message send, no ambiguity hereTest
: might be a message send or a constant; resolution rules say it is a constantUpvotes: 7