Mega Code Archive

 
Categories / Ruby / Class
 

Add a singleton method to a instance object

module Greeter    def hi       "hello"    end end                     # A silly module s = "string object" s.extend(Greeter)       # Add hi as a singleton method to s s.hi                    # => "hello" String.extend(Greeter)  # Add hi as a class method of String String.hi               # => "hello"