hexa 0.1.4 → 0.2.0
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/README.md +36 -14
- data/lib/hexa/core_ext/fixnum.rb +54 -0
- data/lib/hexa/objects/cube.rb +35 -0
- data/lib/hexa/shapes/circle.rb +1 -1
- data/lib/hexa/version.rb +2 -2
- metadata +4 -2
data/README.md
CHANGED
@@ -1,29 +1,51 @@
|
|
1
1
|
# Hexa
|
2
2
|
|
3
|
-
|
3
|
+
Hexa is a gem which provides some objects to work on my math works. There are shapes and objects like circles, squares or cylinders. Each object has its own set of methods like `perimeter`, `volume` or `area`.
|
4
4
|
|
5
|
-
|
5
|
+
There are also extended core objects such as `Fixnum`. They have new methods to simplify operations such as `!`, `prime?` or `sqrt`.
|
6
6
|
|
7
|
-
|
7
|
+
For the moment, the project isn't very complete. You can contribute if you want!
|
8
8
|
|
9
|
-
|
9
|
+
## Installation
|
10
10
|
|
11
|
-
|
11
|
+
If you want to install hexa and start use it:
|
12
12
|
|
13
|
-
|
13
|
+
```
|
14
|
+
$ gem install hexa
|
15
|
+
```
|
14
16
|
|
15
|
-
|
17
|
+
Now you can use hexa into IRB or a Ruby file:
|
16
18
|
|
17
|
-
|
19
|
+
```ruby
|
20
|
+
require "hexa"
|
21
|
+
```
|
18
22
|
|
19
23
|
## Usage
|
20
24
|
|
21
|
-
|
25
|
+
Hexa comes with a set of objects. Objects (Ruby objects) are divided into shapes and objects. Main objects (Ruby) are Circle, Square, Cylinder and Cube. Each constructor accept a Hash of options. Here an example of a IRB session:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
>> c = Circle.new :radius => 10
|
29
|
+
=> #<Circle:0x00000001a00360 @radius=10, @diameter=20>
|
30
|
+
>> c.diameter
|
31
|
+
=> 20
|
32
|
+
>> c.area
|
33
|
+
=> 314.1592653589793
|
34
|
+
```
|
35
|
+
|
36
|
+
Other objects (such as Cylinder) allows you to pass other object to set attributes.
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
>> circle = Circle.new :diameter => 10
|
40
|
+
=> #<Circle:0x00000001a03b28 @radius=5, @diameter=10>
|
41
|
+
>> cylinder = Cylinder.new :radius => circle, :height => 40
|
42
|
+
=> #<Cylinder:0x00000001a0b210 @radius=5, @diameter=10, @height=40>
|
43
|
+
```
|
22
44
|
|
23
45
|
## Contributing
|
24
46
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
47
|
+
If you want to contribute, just fork the project then write and commit your patch. Finally, you can open a pull-request.
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
Hexa is released under the MIT license. See the `LICENSE` file for more information.
|
@@ -0,0 +1,54 @@
|
|
1
|
+
class Fixnum
|
2
|
+
|
3
|
+
# Allows tou to know the factorial of self
|
4
|
+
# @return [Fixnum] factorial of self
|
5
|
+
def factorial
|
6
|
+
if self == 1 or self == 0
|
7
|
+
return 1
|
8
|
+
else
|
9
|
+
return self * (self - 1).factorial
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
alias :! :factorial
|
14
|
+
|
15
|
+
# @return [Float] the square root of self
|
16
|
+
def sqrt
|
17
|
+
Math.sqrt(self)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Predicate method which allows you to know if the number is a prime number
|
21
|
+
# @return [Boolean]
|
22
|
+
def prime?
|
23
|
+
if self < 2
|
24
|
+
false
|
25
|
+
elsif self == 2
|
26
|
+
true
|
27
|
+
elsif self % 2 == 0
|
28
|
+
false
|
29
|
+
else
|
30
|
+
i = 3
|
31
|
+
while i*i <= self
|
32
|
+
return false if self % i == 0
|
33
|
+
i += 2
|
34
|
+
end
|
35
|
+
return true
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @return [Fixnum] the opposite of self
|
40
|
+
def opposite
|
41
|
+
- self
|
42
|
+
end
|
43
|
+
|
44
|
+
# @return [Float] inverse of self
|
45
|
+
def inverse
|
46
|
+
if self == 0
|
47
|
+
return 0
|
48
|
+
else
|
49
|
+
return 1 / self
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
alias :reverse :inverse
|
54
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class Cube
|
2
|
+
|
3
|
+
attr_accessor :side
|
4
|
+
|
5
|
+
# Create a new Cube object
|
6
|
+
# @param [Hash] you should specify the :side option. You can either pass
|
7
|
+
# a Fixnum or a Square object.
|
8
|
+
# @return [Cube] a new Cube object
|
9
|
+
def initialize(options = {})
|
10
|
+
if options[:side]
|
11
|
+
if options[:side].instance_of?(Square)
|
12
|
+
@side = options[:side].side
|
13
|
+
elsif options[:side].instance_of?(Fixnum)
|
14
|
+
@side = options[:side]
|
15
|
+
else
|
16
|
+
@side = 1
|
17
|
+
end
|
18
|
+
else
|
19
|
+
@side = 1
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
# @return [Fixnum] the volume of the cube
|
25
|
+
def volume
|
26
|
+
@side ** 3
|
27
|
+
end
|
28
|
+
|
29
|
+
# Same behavior as volume but you can call it without a Cube instance
|
30
|
+
# like Cube::volume(10) => 1000
|
31
|
+
# @return [Fixnum] the volume of a cube with a given
|
32
|
+
def self.volume(side = 1)
|
33
|
+
side ** 3
|
34
|
+
end
|
35
|
+
end
|
data/lib/hexa/shapes/circle.rb
CHANGED
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.2.0
|
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-23 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,8 @@ files:
|
|
25
25
|
- Rakefile
|
26
26
|
- hexa.gemspec
|
27
27
|
- lib/hexa.rb
|
28
|
+
- lib/hexa/core_ext/fixnum.rb
|
29
|
+
- lib/hexa/objects/cube.rb
|
28
30
|
- lib/hexa/objects/cylinder.rb
|
29
31
|
- lib/hexa/shapes/circle.rb
|
30
32
|
- lib/hexa/shapes/square.rb
|