Project

General

Profile

Actions

Bug #11867

closed

overriden constant instance variable mismatch

Added by dingoegret (dingo egret) over 8 years ago. Updated over 4 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) over 8 years ago

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

Exactly.

What do you expect instead?

Updated by shugo (Shugo Maeda) over 8 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) about 8 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.

Actions #4

Updated by jeremyevans0 (Jeremy Evans) over 4 years ago

  • Status changed from Feedback to Closed
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0