Class: Cylinder

Inherits:
Object
  • Object
show all
Defined in:
lib/hexa/objects/cylinder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

Parameters:

  • you (Hash)

    can pass the :radius, the :diameter and the :height



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(options = {})

  radius = options[:radius] || nil
  diameter = options[:diameter] || nil
	height = options[: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

#diameterObject

Returns the value of attribute diameter.



3
4
5
# File 'lib/hexa/objects/cylinder.rb', line 3

def diameter
  @diameter
end

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/hexa/objects/cylinder.rb', line 3

def height
  @height
end

#radiusObject

Returns the value of attribute radius.



3
4
5
# File 'lib/hexa/objects/cylinder.rb', line 3

def radius
  @radius
end

Instance Method Details

#volumeFixnum

Return the volume of the cylinder thanks to the Math::PI * (self.radius ** 2) * self.height

Returns:

  • (Fixnum)

    cylinder volume



55
56
57
# File 'lib/hexa/objects/cylinder.rb', line 55

def volume
	Math::PI * (@radius ** 2) * @height	
end