Posted By

Shamaoke on 02/26/11


Tagged

ruby tap


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

WaxWing


Object#tap


 / Published in: Ruby
 

The Object#tap method can be plugged between elements of a method chain and perform various operations on an intermediate result of a calculation.

  1. # encoding: utf-8
  2.  
  3. 'one two three'
  4. .split .tap { |x| p x }
  5. .collect(&:upcase) .tap { |x| p x }
  6. .join(' ') .tap { |x| p x }
  7.  
  8. #=> ["one", "two", "three"]
  9. # ["ONE", "TWO", "THREE"]
  10. # "ONE TWO THREE"

Report this snippet  

You need to login to post a comment.