Hacker News new | ask | show | jobs
by zelphirkalt 301 days ago
Doesn't Ruby essentially already have namespaces, in terms of having modules? If one has proper modules, why would one ever need an alternative, weaker, concept for referring to things?
1 comments

To make sure code loaded from gems doesn’t shadow the namespace of the application.
Right. Today Ruby has essentially a global namespace, where every defined module/class/const is put in the same "global dumping ground" and can override/"monkey patch" each other.

Ruby 3.5 will introduce a new language keyword "namespace" that scopes behavior to that namespace.

  class Foo
    def foo; puts "foo"; end
  end

  namespace Bar
    class Foo
      def foo; puts "bar"; end
    end

    Foo.new.foo #=> "bar"
  end

  Foo.new.foo #=> "foo"
Fun times.

This is intended for isolated code loading similar to "modules" in Python or ES6, but I am worried it will be abused badly. I'm also unsure whether they will add a "use Namespace" construct...

See here: https://bugs.ruby-lang.org/issues/21311