[Feedback] attr_reader doesn't warn on a uninitialized instance variable

Issue #9815 has been updated by Usaku NAKAMURA.

Status changed from Closed to Feedback


Bug #9815: attr_reader doesn’t warn on a uninitialized instance variable
https://bugs.ruby-lang.org/issues/9815#change-47554

  • Author: Koichi Sasada
  • Status: Feedback
  • Priority: Normal
  • Assignee: Yukihiro M.
  • Category: core
  • Target version: current: 2.2.0
  • ruby -v: confirmed by 1.9.3-2.2.0
  • Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN

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 は、同じ意味だと思って居たのでびっくりした次第です。

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs