Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including our method to the integer class

Is it possible in Ruby to create a module which will have one method (for example: a math method for computing lcd), and include that method to Integer class, so that I can do after something like this

6.lcd(3)

If it's possible, how can I include it?

like image 380
Xerath Avatar asked Dec 19 '25 04:12

Xerath


1 Answers

You can simply do something like this, without using a module:

class Fixnum
  def lcd(nr)
    self * nr
  end
end

puts 3.lcd(2) # -> 6
like image 66
Sandro Paganotti Avatar answered Dec 20 '25 21:12

Sandro Paganotti