Last active
August 29, 2015 13:56
-
-
Save Integralist/9251201 to your computer and use it in GitHub Desktop.
In Ruby using the `<<` operator as a method identifier has special meaning. It allows us to call the method without using a period (so we can do `foo << "abc"` and not have to do `foo.<< "abc"`)
This file contains 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
class Foo | |
def <<(something) | |
puts something | |
end | |
def say(something) | |
puts something | |
end | |
end | |
foo = Foo.new | |
foo.<<("hi") # => hi | |
foo.<< "hi" # => hi | |
foo << "hi" # => hi | |
foo.say("hi") # => hi | |
foo.say "hi" # => hi | |
foo say "hi" # => NoMethodError: undefined method `say' for main:Object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment