define class methods by using a class within a class's singleton class like the code : Singletons « Design Patterns « Ruby






define class methods by using a class within a class's singleton class like the code


class Area
  class << self
    def rect( length, width, units="inches" )
      area = length*width
      printf( "The area of this rectangle is %.2f %s.", area, units )
      sprintf( "%.2f", area )
    end
  end
end

Area.rect(10, 10) # The area of this rectangle is 100.00 inches.=> "100.00"

 








Related examples in the same category

1.Define a singleton method, one that is tied to a single object
2.Singleton a class
3.Classes as Singletons
4.Modules as Singletons
5.include Singleton