Skip to content

Instantly share code, notes, and snippets.

@hopsoft
Created January 11, 2011 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hopsoft/774638 to your computer and use it in GitHub Desktop.
Save hopsoft/774638 to your computer and use it in GitHub Desktop.
Kernel Method - Illustrates how to add functionality to all objects
# 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