Project

General

Profile

Actions

Bug #9815

closed

attr_reader doesn't warn on a uninitialized instance variable

Added by ko1 (Koichi Sasada) almost 10 years ago. Updated over 3 years ago.

Status:
Rejected
Target version:
-
ruby -v:
confirmed by 1.9.3-2.2.0
[ruby-dev:48184]
Tags:

Description

attr_reader で定義したメソッドでは、-w 時に、未設定の警告「warning: instance variable @bar not initialized」みたいなのが出ないんですが、そういうもんでしょうか。

class C
  attr_reader :foo
  def bar; @bar; end
end
C.new.foo
C.new.bar #=> t.rb:4: warning: instance variable @bar not initialized

この例では、foo と bar は、同じ意味だと思って居たのでびっくりした次第です。

Updated by nobu (Nobuyoshi Nakada) almost 10 years ago

  • Status changed from Open to Closed

意図的なものです。
rb_ivar_getrb_attr_getの差です。

Updated by ko1 (Koichi Sasada) almost 10 years ago

どういう意図でしたっけ。
ちょっとググったけど見当たらず。

Updated by usa (Usaku NAKAMURA) over 9 years ago

  • Status changed from Closed to Feedback
Actions #4

Updated by naruse (Yui NARUSE) about 6 years ago

  • Target version deleted (2.2.0)
Actions #5

Updated by ko1 (Koichi Sasada) about 4 years ago

  • Status changed from Feedback to Rejected

Updated by Eregon (Benoit Daloze) over 3 years ago

What was the rationale here to have different behavior?

Which C function is used by attr_reader seems an implementation detail.

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0