Class: Square

Inherits:
Object
  • Object
show all
Defined in:
lib/hexa/shapes/square.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

Parameters:

  • you (Hash, Fixnum)

    can pass a :side option with a fixnum



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

#sideObject 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

Parameters:

  • the (Fixnum)

    square’s side’s length

Returns:

  • (Fixnum)

    area defined with the given side’s length ^ 2



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

Parameters:

  • the (Fixnum)

    length of the square’s side, default is 1

Returns:

  • (Fixnum)

    perimiter of the sqare



31
32
33
# File 'lib/hexa/shapes/square.rb', line 31

def self.perimeter(side = 1)
	4 * side
end

Instance Method Details

#areaFixnum

Returns are of the squre defined by self.side ^ 2.

Returns:

  • (Fixnum)

    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

#perimeterFixnum

Returns perimeter of the square defined with 4 * self.side.

Returns:

  • (Fixnum)

    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