Skip to content

Instantly share code, notes, and snippets.

@anshul
Created November 25, 2013 06:02
Show Gist options
  • Save anshul/7636988 to your computer and use it in GitHub Desktop.
Save anshul/7636988 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby -w
puts "hello" << "meow" << "rofl"
class String
def <<(arg)
puts "#{self.to_s} called with #{arg.inspect}"
"#{self}#{arg}"
end
end
puts "hello" << "meow" << "rofl"
# Result with ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.0] =>
#
# hellomeowrofl
# test_associativity.rb:5: warning: method redefined; discarding old <<
# hello called with "meow"
# hellomeow called with "rofl"
# hellomeowrofl
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment