Ruby 4.0 リファレンスマニュアル

instance method Object#then

yield_self {|x| ... } -> object[permalink][rdoc][edit]
yield_self -> Enumerator
then {|x| ... } -> object
then -> Enumerator

self を引数としてブロックを評価し、ブロックの結果を返します。

p 3.next.then {|x| x**x }.to_s           # => "256"
p "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 を返します。例えば条件によって値を捨てるのに使えます。

# 条件にあうので何もしない
p 1.yield_self.detect(&:odd?)          # => 1
# 条件に合わないので値を捨てる
p 2.yield_self.detect(&:odd?)          # => nil

[SEE_ALSO] Object#tap