Ruby: Calling class method from instance

Posted by Peter on Stack Overflow See other posts from Stack Overflow or by Peter
Published on 2010-03-27T01:48:01Z Indexed on 2010/03/27 1:53 UTC
Read the original article Hit count: 277

Filed under:

In Ruby, how do you call a class method from one of that class's instances? Say I have

class Truck
  def self.default_make
    # Class method.
    "mac"
  end

  def initialize
    # Instance method.
    Truck.default_make  # gets the default via the class's method.
    # But: I wish to avoid mentioning Truck. Seems I'm repeating myself.
  end
end

the line Truck.default_make retrieves the default. But is there a way of saying this without mentioning Truck? It seems like there should be.

© Stack Overflow or respective owner

Related posts about ruby