hexa 0.0.1 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/hexa/objects/cylinder.rb +59 -0
- data/lib/hexa/shapes/circle.rb +21 -0
- data/lib/hexa/shapes/square.rb +20 -0
- data/lib/hexa/version.rb +4 -1
- metadata +5 -2
@@ -0,0 +1,59 @@
|
|
1
|
+
class Cylinder
|
2
|
+
|
3
|
+
attr_accessor :radius, :diameter, :height
|
4
|
+
|
5
|
+
# Create a new Cylinder object
|
6
|
+
# @param [Hash] you can pass the :radius, the :diameter and the :height
|
7
|
+
# options. You can pass both a Fixnum or a Circle object to both the :radius
|
8
|
+
# and the :diameter options. If you pass a Circle object, the method will fetch
|
9
|
+
# the radius or diameter attribute
|
10
|
+
# @return [Cylinder] a new Cylinder object
|
11
|
+
def initialize(options = {})
|
12
|
+
|
13
|
+
radius = options[:radius] || nil
|
14
|
+
diameter = options[:diameter] || nil
|
15
|
+
height = options[:height] || nil
|
16
|
+
|
17
|
+
# Define the cylinder radius
|
18
|
+
if radius
|
19
|
+
if radius.instance_of?(Circle)
|
20
|
+
@radius = radius.send(:radius)
|
21
|
+
elsif radius.instance_of?(Fixnum)
|
22
|
+
@radius = radius
|
23
|
+
else
|
24
|
+
@radius = 1
|
25
|
+
end
|
26
|
+
else
|
27
|
+
@radius = 1
|
28
|
+
end
|
29
|
+
|
30
|
+
# Define the cylinder diameter
|
31
|
+
if radius
|
32
|
+
@diameter = @radius * 2
|
33
|
+
else
|
34
|
+
if diameter
|
35
|
+
if diameter.instance_of?(Circle)
|
36
|
+
@diameter = diameter.send(:diameter)
|
37
|
+
elsif diameter.instance_of?(Fixnum)
|
38
|
+
@diameter = diameter
|
39
|
+
else
|
40
|
+
@diameter = 2
|
41
|
+
end
|
42
|
+
else
|
43
|
+
@diameter = 2
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
# Define the cylinder height
|
48
|
+
@height = height || 1
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
# Return the volume of the cylinder thanks to the
|
53
|
+
# Math::PI * (self.radius ** 2) * self.height
|
54
|
+
# @return [Fixnum] cylinder volume
|
55
|
+
def volume
|
56
|
+
Math::PI * (@radius ** 2) * @height
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
class Circle
|
2
|
+
|
3
|
+
attr_accessor :radius, :diameter
|
4
|
+
|
5
|
+
# Constructor for the Circle object
|
6
|
+
# @param [Hash] options you can specify :radius or :diameter
|
7
|
+
# @return [Circle] a new Circle object with the specified radius or diameter
|
8
|
+
def initialize(options = {})
|
9
|
+
if options[:radius]
|
10
|
+
@radius = options[:radius]
|
11
|
+
@diameter = options[:radius] * 2
|
12
|
+
elsif options[:diameter]
|
13
|
+
@radius = options[:diameter] / 2
|
14
|
+
@diameter = options[:diameter]
|
15
|
+
else
|
16
|
+
@radius = 1
|
17
|
+
@diamter = 2
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Square
|
2
|
+
|
3
|
+
attr_accessor :side
|
4
|
+
|
5
|
+
# Constructor for the Square object
|
6
|
+
# @param [Hash, Fixnum] you can pass a :side option with a fixnum
|
7
|
+
# as the value of the :side key or just pass a Fixnum, if you don't
|
8
|
+
# pass anything, the default side length is 1
|
9
|
+
# @return [Square] a new Square object
|
10
|
+
def initialize(arg = {})
|
11
|
+
if arg.instance_of?(Hash) and arg[:side]
|
12
|
+
@side = arg[:side]
|
13
|
+
elsif arg.instance_of?(Fixnum)
|
14
|
+
@side = arg
|
15
|
+
else
|
16
|
+
@side = 1
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
data/lib/hexa/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hexa
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-08-
|
12
|
+
date: 2012-08-21 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A personnal library to check my math homeworks
|
15
15
|
email:
|
@@ -25,6 +25,9 @@ files:
|
|
25
25
|
- Rakefile
|
26
26
|
- hexa.gemspec
|
27
27
|
- lib/hexa.rb
|
28
|
+
- lib/hexa/objects/cylinder.rb
|
29
|
+
- lib/hexa/shapes/circle.rb
|
30
|
+
- lib/hexa/shapes/square.rb
|
28
31
|
- lib/hexa/version.rb
|
29
32
|
homepage: ''
|
30
33
|
licenses: []
|