Add a singleton method to a instance object : singleton method « Class « Ruby






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"

 








Related examples in the same category

1.Add singleton method to a string instance
2.Singleton method not copied
3.Use extend to include a module to create single methods