Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Kernel#yield_self

obj.yield_self {|x| block } → an_object

Yields self to the block and returns the result of the block.

Example
"my string".yield_self {|s| s.upcase }   #=> "MY STRING"

Good usage for then is value piping in method chains:

Example
require 'open-uri'
require 'json'

construct_url(arguments).
  then {|url| URI(url).read }.
  then {|response| JSON.parse(response) }
See also #y #warn also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0