Created
July 16, 2012 19:58
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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