Project

General

Profile

Actions

Bug #11867

closed

overriden constant instance variable mismatch

Bug #11867: overriden constant instance variable mismatch

Added by dingoegret (dingo egret) almost 10 years ago. Updated over 6 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]
[ruby-core:72455]

Description

A = Struct.new(:a)

A.new(1)
#<struct A a=1>

class A
  def initialize(b)
    @b = b
  end
end

A.new(1)
#<struct A a=nil>

Is this expected?

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago Actions #1 [ruby-core:72460]

  • Description updated (diff)
  • Status changed from Open to Feedback

Exactly.

What do you expect instead?

Updated by shugo (Shugo Maeda) almost 10 years ago Actions #2 [ruby-core:72462]

dingo egret wrote:

class A
  def initialize(b)
    @b = b
  end
end

You should call super if you want to set the member a.

class A
  def initialize(b)
    super(b)
    @b = b
  end
end

Updated by dingoegret (dingo egret) almost 10 years ago Actions #3 [ruby-core:72600]

Nobuyoshi Nakada wrote:

Exactly.

What do you expect instead?

I expected the latest constant to set its @b property. If not Class A, at the very least I expected Struct :a to get set. Is this not the intuitive thing to expect? People send arguments based on param ordering in constant definition. This is totally bypassing that expectation.

Updated by jeremyevans0 (Jeremy Evans) over 6 years ago Actions #4

  • Status changed from Feedback to Closed
Actions

Also available in: PDF Atom