Class: Dice::Polyhedron

Inherits:
Object
  • Object
show all
Defined in:
lib/theory_of_probability.rb

Overview

Class represents a polyhedron csides - number or sides sides - array of sides(unusual for custom polyhedrons)

Instance Method Summary collapse

Constructor Details

#initialize(sides) ⇒ Polyhedron

initializing polyhedron’s variables there are two ways how to create it 1: by number (6) - creates polyhedron with 6 sides [1,2,3,4,5,6] 2: by array ([1,3,5]) - creates polyhedron with 3 sides [1,3,5]



83
84
85
86
87
88
89
90
91
92
93
# File 'lib/theory_of_probability.rb', line 83

def initialize(sides)
  @csides = 1
  @sides = [1]
  if sides.class == Integer and sides > 1
    @csides = sides
    (2..sides).each {|i| @sides << i}
  elsif sides.class == Array and sides.size > 0
    @csides = sides.size
    @sides = sides.sort
  end
end

Instance Method Details

#csidesObject



66
67
68
# File 'lib/theory_of_probability.rb', line 66

def csides
  @csides
end

#sidesObject



70
71
72
# File 'lib/theory_of_probability.rb', line 70

def sides
  @sides
end

#throwObject

ability to throw a polyhedron



97
98
99
# File 'lib/theory_of_probability.rb', line 97

def throw
  @sides[rand(0..@csides-1)]
end

#to_sObject



74
75
76
# File 'lib/theory_of_probability.rb', line 74

def to_s
  sides
end