Issue #5632 has been updated by boris_stitnicky (Boris Stitnicky).


Well, it feels weird to disagree with the creator of the language you decided to use for your career, but matz, I think that in your example, in

  class B
    A = 42
  end

it is very clear, that you are performing an assignment. There is no need for warning.

But my problem was, that I thought I was opening an existing class using

  class X
    def goodbye, puts 'goodbye' end
  end

that is, not performing assignment to X, but changing the object to which X refers.
But instead, "class X" statement performed a hidden assignment of a newly created
class to X. I believe that in this case, warning is needed. The warning could be
avoided by eg. explicitly assigning
  X = Class.new

and then working with it:
  class X
    # do the definitions
  end

The warning should activate only when X was pre-assigned and hidden assignment is
being performed by 'class' statement.
----------------------------------------
Feature #5632: Attempt to open included class shades it instead.
https://bugs.ruby-lang.org/issues/5632#change-26538

Author: boris_stitnicky (Boris Stitnicky)
Status: Assigned
Priority: Normal
Assignee: mame (Yusuke Endoh)
Category: 
Target version: 3.0


# Hello everyone. I'm not a very advanced ruby user, and I
# would like to provide and outsider report on certain ruby
# behavior that might surprise newbies.

module A
  class X
    def hello; puts 'hello' end
  end
end

module B
  include A
end

B::X.new.hello
=> hello
# As expected.

# But when I tried to add new functionality to X, ...
module B
  class X
    def goodbye; puts 'goodbye' end
  end
end

B::X.new.hello
=> NoMethodError

# I was surprised, that my .hello method disappeared,
# when all I was trying to do, was to improve X in B.
# I actually somehow expected to work on a subclass
# of X, like this:

module C
  include A
  class X < X
    def goodbye; puts 'goodbye' end
  end
end

# My suggestions are:
# 1. I consider 'class X < X' syntax a little bit
#    mysterious. How about making this a default
#    behavior for 'class X' statements?
# 2. If the above is not considered beneficial, I
#    would welcome if 'class X' statement warned
#    when shadowing an existing name. People might
#    often assume that they are opening an existing
#    class, rather than getting a brand new one
#    shadowing the previous one. If people really
#    want a brand new shadowing class without warning
#    they could use explicit 'X = Class.new'.


-- 
http://bugs.ruby-lang.org/