"Object" is called a class in Ruby

Does it have flaws if I use Object as a name for my internal module?

module Some
  class Object; end
end
+3
source share
3 answers

Actually, this code should work without problems, since it is in the module and, therefore, is named. 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

Perhaps this can be confusing or ambiguous if you include Some in another class or module within which Object will refer to Some :: Object. However, it will still not affect anything outside this class or module.

+10
source

There are some pitfalls, but it works. If you do this, you will extend the Object class, which is already in Ruby.

class Object
  def hello
  end
end

, , .

module Foo
  class Object
    # ...
  end
end

, .

, ::Object, Object. , .

, , . , Some::Object, , , Some::Record, Some::Entity .

+3

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

-2
source

Source: https://habr.com/ru/post/1708460/


All Articles