Include a module into a class : include « Class « Ruby






Include a module into a class


#!/usr/bin/env ruby

module Dice

  # virtual roll of a pair of dice
  def roll
    r_1 = rand(6); r_2 = rand(6)
    r1 = r_1>0?r_1:1; r2 = r_2>0?r_2:6
    total = r1+r2
    printf( "You rolled %d and %d (%d).\n", r1, r2, total )
    total
  end

end

class Game
 include Dice
end

g = Game.new
g.roll

 








Related examples in the same category

1.Include module with same method name
2.Add extension from a module for an object not class
3.Extending Specific Objects with Modules