Skip to content

Instantly share code, notes, and snippets.

@brianjlandau
Created July 16, 2012 19:58
Show Gist options
  • Select an option

  • Save brianjlandau/3124674 to your computer and use it in GitHub Desktop.

Select an option

Save brianjlandau/3124674 to your computer and use it in GitHub Desktop.
In ruby `+=` and `<<` behave slightly differently inside a `tap` block, because one modifies the object and the other reassigns a new object to the same named variable.
irb(main):001:0> my_text = ''
=> ""
irb(main):002:0> my_text += "this is a test"
=> "this is a test"
irb(main):003:0> my_text
=> "this is a test"
irb(main):004:0> my_text << " another test"
=> "this is a test another test"
irb(main):005:0> my_text
=> "this is a test another test"
irb(main):006:0> ''.tap do |text|
irb(main):007:1* text += "this is a test"
irb(main):008:1> end
=> ""
irb(main):009:0> ''.tap do |text|
irb(main):010:1* text << "this is another test"
irb(main):011:1> end
=> "this is another test"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment