Skip to content

Instantly share code, notes, and snippets.

@Integralist
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Integralist/9251201 to your computer and use it in GitHub Desktop.
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"`)
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