Skip to content

Instantly share code, notes, and snippets.

@doodlehaus
Created December 13, 2013 15:06
Show Gist options
  • Save doodlehaus/7945616 to your computer and use it in GitHub Desktop.
Save doodlehaus/7945616 to your computer and use it in GitHub Desktop.
# define a specific string
my_string = "I'm a string!"
# call #reverse
# 1) this first checks to see if my_string has a method called reverse
# 2) it doesn't find one
# 3) it then checks for reverse on my_string's parent class, which is String
# 4) it finds the method and executes it and outputs the following:
my_string.reverse
=> "!gnirts a m'I"
# if we define a singleton method on my_string like so:
def my_string.reverse
"I'm overriding .reverse"
end
# and then call .reverse on my_string
my_string.reverse
# we get the result of the new method and not that of
# the default behavior of String
my_string.reverse
=> "I'm overriding .reverse"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment