Ruby 3.1 リファレンスマニュアル > ライブラリ一覧 > 組み込みライブラリ > Objectクラス > then
yield_self {|x| ... } -> object
[permalink][rdoc]yield_self -> Enumerator
then {|x| ... } -> object
then -> Enumerator
self を引数としてブロックを評価し、ブロックの結果を返します。
3.next.then {|x| x**x }.to_s # => "256"
"my string".yield_self {|s| s.upcase } # => "MY STRING"
値をメソッドチェインのパイプラインに次々と渡すのは良い使い方です。
require 'open-uri'
require 'json'
construct_url(arguments).
yield_self {|url| URI(url).read }.
yield_self {|response| JSON.parse(response) }
ブロックなしで呼び出されたときは Enumerator を返します。例えば条件によって値を捨てるのに使えます。
# 条件にあうので何もしない
1.yield_self.detect(&:odd?) # => 1
# 条件に合わないので値を捨てる
2.yield_self.detect(&:odd?) # => nil
[SEE_ALSO] Object#tap