End of Life Ruby 3.0 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
kernel.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.0.5/kernel.rb, line 144
def yield_self
unless Primitive.block_given_p
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, rb_obj_size)'
end
yield(self)
end
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| open(url).read }. then {|response| JSON.parse(response) }