Skip to content

Instantly share code, notes, and snippets.

@hiroyuki-sato
Last active December 2, 2020 10:32
Show Gist options
  • Select an option

  • Save hiroyuki-sato/ffce54e01cf9d884d61b1d7f9584b379 to your computer and use it in GitHub Desktop.

Select an option

Save hiroyuki-sato/ffce54e01cf9d884d61b1d7f9584b379 to your computer and use it in GitHub Desktop.
forwardable

現象

  • forwadable_test.rb: Ruby2.0だと"piyo"と出る。Ruby 2.7.2だとuninitialized constant Forwardable::INSTANCEになる
  • forwadable_test2.rb: 修正案、ruby 2.0でもruby2.7.2でもpiyoと出力される

確認した動作

ruby 2.7.2で、forwardable_test.rb を実行した場合

	1: from test.rb:24:in `<main>'
/path/to/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/forwardable-1.3.1/lib/forwardable.rb:229:in `piyo': uninitialized constant Forwardable::INSTANCE (NameError)

ruby 2.7.0で、forwardable_test.rb

piyo

疑問

  • なぜ上記のような現象が起きるのか?(Rubyの仕様変更の影響、forwardableの仕様変更、その他)
  • 検討中の解決方法は修正案として良い方法かどうか?

補足

本現象はバルクローダembulkをテストしているときに発見した。 embulkでは、いままでJRuby 9.1.15.0を利用しておりこれを9.2.23.0にアップデートしたところ本現象が発生した。GitHub Issue

#!/usr/bin/env ruby
require 'forwardable'
class Fuga
def piyo
puts "piyo"
end
end
module Hoge
class <<self
INSTANCE = Fuga.new
extend Forwardable
def_delegators 'INSTANCE',
:piyo
end
end
Hoge.piyo
#!/usr/bin/env ruby
require 'forwardable'
class Fuga
def piyo
puts "piyo"
end
end
module Hoge
@INSTANCE = Fuga.new
class <<self
extend Forwardable
def_delegators '@INSTANCE',
:piyo
end
end
Hoge.piyo
#!/usr/bin/env ruby
require 'forwardable'
class Fuga
def piyo
puts "piyo"
end
end
module Hoge
INSTANCE = Fuga.new
class <<self
extend Forwardable
def_delegators :"::Hoge::INSTANCE",
:piyo
end
end
Hoge.piyo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment