How does ruby allow a method and a Class with the same name?

Posted by Daniel Beardsley on Stack Overflow See other posts from Stack Overflow or by Daniel Beardsley
Published on 2010-06-06T20:52:36Z Indexed on 2010/06/06 21:02 UTC
Read the original article Hit count: 179

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?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby