Ruby 2.5 added yield_self
irb> "Hello".yield_self { |str| str + " World" } => "Hello World"
1. How is it different from try in Rails ?
try is not part of Ruby but Rails. Also try’s main purpose is protection against nil hence it doesn’t execute the block if receiver is nil.
irb> nil.yield_self { |obj| "Hello World" } => "Hello World" irb> nil.try { |obj| "Hello World" } => nil
2. How is it different from tab?
tap also is similar to yield_self. It’s part of Ruby itself. The only difference is the value that is returned. tap returns the receiver itself while yield_self returns the output of the block.
irb> "Hello".yield_self { |str| str + " World" } => "Hello World" irb> "Hello".tap { |str| str + " World" } => "Hello"
References
Ruby 2.5 added yield_self
http://blog.bigbinary.com/2017/12/12/ruby-2-5-added-yield_self.html