warning: circular argument reference

【追記】

よいまとめはこっち

qiita.com


v2.1.6 > v2.2.2 の移行より

分かったこと

test.rb

  1 class SomeClass
  2   attr_reader :text
  3 
  4   def initialize
  5     @text = 'test read'
  6   end
  7 
  8   def some_func(text = text) # allocate 'attribute text' if no arg passed
  9     p text
 10   end
 11 end
 12 
 13 SomeClass.new.some_func

v 2.1.6

$ ruby test.rb
"test read"

v 2.2.2

$ ruby test.rb
test.rb:8: warning: circular argument reference - text
nil

v 2.2.2 の場合の解決策

L8 のデフォルト引数を以下通り変更

  8   def some_func(text = text()) # allocate 'attribute text' if no arg passed

(attr* は、アクセッサを定義するための「メソッド」であり

text = text0 と言語表記できるので問題ない)

(v2.1.6 の言語表記が正だったのはなぜ?(いきさつ全く追いきれてない))

Rubyのattr_accessor, attr_reader, attr_writerとは何か -- ぺけみさお

集めた情報

Matz より

bugs.ruby-lang.org

Rails 界 v2.2 移行の話

github.com