Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I alias a static method in Ruby?

Tags:

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
like image 235
chopper draw lion4 Avatar asked Nov 20 '25 23:11

chopper draw lion4


1 Answers

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
like image 119
Marek Lipka Avatar answered Nov 23 '25 22:11

Marek Lipka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!