Class: Cylinder
- Inherits:
-
Object
- Object
- Cylinder
- Defined in:
- lib/hexa/objects/cylinder.rb
Instance Attribute Summary collapse
-
#diameter ⇒ Object
Returns the value of attribute diameter.
-
#height ⇒ Object
Returns the value of attribute height.
-
#radius ⇒ Object
Returns the value of attribute radius.
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ Cylinder
constructor
Create a new Cylinder object options.
-
#volume ⇒ Fixnum
Return the volume of the cylinder thanks to the Math::PI * (self.radius ** 2) * self.height.
Constructor Details
#initialize(options = {}) ⇒ Cylinder
Create a new Cylinder object options. You can pass both a Fixnum or a Circle object to both the :radius and the :diameter options. If you pass a Circle object, the method will fetch the radius or diameter attribute
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/hexa/objects/cylinder.rb', line 11 def initialize( = {}) radius = [:radius] || nil diameter = [:diameter] || nil height = [:height] || nil # Define the cylinder radius if diameter and not radius @radius = diameter / 2 else if radius.instance_of?(Circle) @radius = radius.send(:radius) elsif radius.instance_of?(Fixnum) @radius = radius else @radius = 1 end end # Define the cylinder diameter if radius @diameter = @radius * 2 else if diameter if diameter.instance_of?(Circle) @diameter = diameter.send(:diameter) elsif diameter.instance_of?(Fixnum) @diameter = diameter else @diameter = 2 end else @diameter = 2 end end # Define the cylinder height @height = height || 1 end |
Instance Attribute Details
#diameter ⇒ Object
Returns the value of attribute diameter.
3 4 5 |
# File 'lib/hexa/objects/cylinder.rb', line 3 def diameter @diameter end |
#height ⇒ Object
Returns the value of attribute height.
3 4 5 |
# File 'lib/hexa/objects/cylinder.rb', line 3 def height @height end |
#radius ⇒ Object
Returns the value of attribute radius.
3 4 5 |
# File 'lib/hexa/objects/cylinder.rb', line 3 def radius @radius end |
Instance Method Details
#volume ⇒ Fixnum
Return the volume of the cylinder thanks to the Math::PI * (self.radius ** 2) * self.height
55 56 57 |
# File 'lib/hexa/objects/cylinder.rb', line 55 def volume Math::PI * (@radius ** 2) * @height end |