hexa 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/hexa/objects/cylinder.rb +5 -5
- data/lib/hexa/shapes/circle.rb +12 -0
- data/lib/hexa/shapes/square.rb +29 -0
- data/lib/hexa/version.rb +1 -1
- metadata +1 -1
@@ -10,12 +10,14 @@ class Cylinder
|
|
10
10
|
# @return [Cylinder] a new Cylinder object
|
11
11
|
def initialize(options = {})
|
12
12
|
|
13
|
-
|
14
|
-
|
13
|
+
radius = options[:radius] || nil
|
14
|
+
diameter = options[:diameter] || nil
|
15
15
|
height = options[:height] || nil
|
16
16
|
|
17
17
|
# Define the cylinder radius
|
18
|
-
if radius
|
18
|
+
if diameter and not radius
|
19
|
+
@radius = diameter / 2
|
20
|
+
else
|
19
21
|
if radius.instance_of?(Circle)
|
20
22
|
@radius = radius.send(:radius)
|
21
23
|
elsif radius.instance_of?(Fixnum)
|
@@ -23,8 +25,6 @@ class Cylinder
|
|
23
25
|
else
|
24
26
|
@radius = 1
|
25
27
|
end
|
26
|
-
else
|
27
|
-
@radius = 1
|
28
28
|
end
|
29
29
|
|
30
30
|
# Define the cylinder diameter
|
data/lib/hexa/shapes/circle.rb
CHANGED
@@ -18,4 +18,16 @@ class Circle
|
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
+
# @return [Float] the area of the circle
|
22
|
+
def area
|
23
|
+
Math::PI * (@radius ** 2)
|
24
|
+
end
|
25
|
+
|
26
|
+
# Same behavior as area but can be called just passing the radius
|
27
|
+
# @param [Fixnum] radius of the circle
|
28
|
+
# @return [Float] area of the circle
|
29
|
+
def self.area(radius = 1)
|
30
|
+
Math::PI * (radius ** 2)
|
31
|
+
end
|
32
|
+
|
21
33
|
end
|
data/lib/hexa/shapes/square.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
class Square
|
2
2
|
|
3
3
|
attr_accessor :side
|
4
|
+
alias :length :side
|
4
5
|
|
5
6
|
# Constructor for the Square object
|
6
7
|
# @param [Hash, Fixnum] you can pass a :side option with a fixnum
|
@@ -17,4 +18,32 @@ class Square
|
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
21
|
+
|
22
|
+
# @return [Fixnum] perimeter of the square defined with 4 * self.side
|
23
|
+
def perimeter
|
24
|
+
4 * @side
|
25
|
+
end
|
26
|
+
|
27
|
+
# Same behavior as perimeter but you don't need to instance
|
28
|
+
# a new square object ; just call Square::perimeter(1) for instance
|
29
|
+
# @param [Fixnum] the length of the square's side, default is 1
|
30
|
+
# @return [Fixnum] perimiter of the sqare
|
31
|
+
def self.perimeter(side = 1)
|
32
|
+
4 * side
|
33
|
+
end
|
34
|
+
|
35
|
+
# @return [Fixnum] are of the squre defined by self.side ^ 2
|
36
|
+
def area
|
37
|
+
@side ** 2
|
38
|
+
end
|
39
|
+
|
40
|
+
|
41
|
+
# Same behavior as area but you don't have to create a new square object
|
42
|
+
# just call Square::area precising the side's length as a parameter
|
43
|
+
# @param [Fixnum] the square's side's length
|
44
|
+
# @return [Fixnum] area defined with the given side's length ^ 2
|
45
|
+
def self.area(side = 1)
|
46
|
+
side ** 2
|
47
|
+
end
|
48
|
+
|
20
49
|
end
|
data/lib/hexa/version.rb
CHANGED