tags:

views:

137

answers:

3

Does it have any drawbacks if I use Object as a name for my class inside module?

module Some
  class Object; end
end
A: 

Object is a reserved word in Ruby, so you should not use it as a name for your class.

Marc W
Object is not a reserved word in Ruby - it's a globally defined constant that refers to a particular class, much like String, Hash, etc. Reserved words in Ruby are things like `if`, `return`, etc.
Greg Campbell
+9  A: 

Actually, this code ought to work with no problems, since it's in a module and thus namespaced. For a simple test:

module Some
  class Object
    def foo
      "bar"
    end
  end
end

Some::Object.new.foo # "bar"
Some::Object.new.class # "Some::Object"

# And it doesn't pollute the global namespaced Object class:
Object.new.respond_to?(:foo) # false

It could potentially be confusing or ambiguous, however, if you include Some in another class or module, inside which Object will refer to Some::Object. It still won't affect anything outside that class or module, though.

Greg Campbell
This is the correct answer: The only real drawback is that it's confusing.
Chuck
+2  A: 

There are some pitfalls, but it works. If you do this, you'll expand the Object class that is already in Ruby.

class Object
  def hello
  end
end

When you namespace it you'll create a new class, though, in that namespace.

module Foo
  class Object
    # ...
  end
end

Technically speaking, this is not a problem.

One downside is that you have to use ::Object when you want to refer to the built-in Object class. You don't doo that very often, though, so it's not a big problem.

The other problem is that can be very confusing for other developers, which you should take into consideration. It's hard to tell from your snippet what this Some::Object class of yours does, but perhaps Some::Record, Some::Entity makes more sense.

August Lilleaas