Project

General

Profile

Bug #11867

overriden constant instance variable mismatch

Added by dingoegret (dingo egret) over 3 years ago. Updated 28 days ago.

Status:
Closed
Priority:
Normal
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?

History

Updated by nobu (Nobuyoshi Nakada) over 3 years ago

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

Exactly.

What do you expect instead?

Updated by shugo (Shugo Maeda) over 3 years ago

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) over 3 years ago

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.

#4

Updated by jeremyevans0 (Jeremy Evans) 28 days ago

  • Status changed from Feedback to Closed

Also available in: Atom PDF