[ ruby ] Object.tap : one of the ruby lambda chain function

“tap into” a method chain, in order to perform operations on intermediate results within the chain

Usage

> (1..10).tap { |x| puts "original: #{x}" }.to_a.
tap { |x| puts "array: #{x}" }.
select { |x| x%2 == 0 }.
tap { |x| puts "evens: #{x}" }.
map { |x| x*x }.
tap { |x| puts "squares: #{x}" }

Result

original: 1..10
array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
evens: [2, 4, 6, 8, 10]
squares: [4, 16, 36, 64, 100]
=> [4, 16, 36, 64, 100]

ref : https://apidock.com/rails/Object/tap

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s