chopper draw lion4
chopper draw lion4

Reputation: 13497

How do I alias a static method in Ruby?

I am putting this code into a wrapper class and therefore need all helper methods to be static. Everything is working besides my seconds method. How do I alias :seconds to a static method?

  def self.minutes
    (rand(58) + 1).to_s
  end

  def self.hours
    (rand(22) + 1).to_s
  end

  alias :seconds :minutes

Upvotes: 3

Views: 905

Answers (1)

Marek Lipka
Marek Lipka

Reputation: 51151

Static methods are really instance methods of class's eigenclass, so you can do:

class << self
  def minutes
    (rand(58) + 1).to_s
  end

  def hours
    (rand(22) + 1).to_s
  end

  alias :seconds :minutes
end

Upvotes: 7

Related Questions