Created
January 11, 2011 16:12
-
-
Save hopsoft/774638 to your computer and use it in GitHub Desktop.
Kernel Method - Illustrates how to add functionality to all objects
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
# add a kernel method to make it available to all objects | |
# this example also serves to illustrate that everything in Ruby is an object | |
module Kernel | |
def say_hello | |
puts "hello from #{self.class.name}" | |
end | |
end | |
# usage | |
Class.say_hello # => hello from Class | |
Object.say_hello # => hello from Class | |
Object.new.say_hello # => hello from Object | |
1.say_hello # => hello from Fixnum | |
"".say_hello # => hello from String |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment