Class: Square
- Inherits:
-
Object
- Object
- Square
- Defined in:
- lib/hexa/shapes/square.rb
Instance Attribute Summary collapse
-
#side ⇒ Object
(also: #length)
Returns the value of attribute side.
Class Method Summary collapse
-
.area(side = 1) ⇒ Fixnum
Same behavior as area but you don’t have to create a new square object just call Square::area precising the side’s length as a parameter.
-
.perimeter(side = 1) ⇒ Fixnum
Same behavior as perimeter but you don’t need to instance a new square object ; just call Square::perimeter(1) for instance.
Instance Method Summary collapse
-
#area ⇒ Fixnum
Are of the squre defined by self.side ^ 2.
-
#initialize(arg = {}) ⇒ Square
constructor
Constructor for the Square object as the value of the :side key or just pass a Fixnum, if you don’t pass anything, the default side length is 1.
-
#perimeter ⇒ Fixnum
Perimeter of the square defined with 4 * self.side.
Constructor Details
#initialize(arg = {}) ⇒ Square
Constructor for the Square object as the value of the :side key or just pass a Fixnum, if you don’t pass anything, the default side length is 1
11 12 13 14 15 16 17 18 19 |
# File 'lib/hexa/shapes/square.rb', line 11 def initialize(arg = {}) if arg.instance_of?(Hash) and arg[:side] @side = arg[:side] elsif arg.instance_of?(Fixnum) @side = arg else @side = 1 end end |
Instance Attribute Details
#side ⇒ Object Also known as: length
Returns the value of attribute side.
3 4 5 |
# File 'lib/hexa/shapes/square.rb', line 3 def side @side end |
Class Method Details
.area(side = 1) ⇒ Fixnum
Same behavior as area but you don’t have to create a new square object just call Square::area precising the side’s length as a parameter
45 46 47 |
# File 'lib/hexa/shapes/square.rb', line 45 def self.area(side = 1) side ** 2 end |
.perimeter(side = 1) ⇒ Fixnum
Same behavior as perimeter but you don’t need to instance a new square object ; just call Square::perimeter(1) for instance
31 32 33 |
# File 'lib/hexa/shapes/square.rb', line 31 def self.perimeter(side = 1) 4 * side end |
Instance Method Details
#area ⇒ Fixnum
Returns are of the squre defined by self.side ^ 2.
36 37 38 |
# File 'lib/hexa/shapes/square.rb', line 36 def area @side ** 2 end |
#perimeter ⇒ Fixnum
Returns perimeter of the square defined with 4 * self.side.
23 24 25 |
# File 'lib/hexa/shapes/square.rb', line 23 def perimeter 4 * @side end |