Class: LinearFunction

Inherits:
Object
  • Object
show all
Defined in:
lib/linear_function.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slope, y_int) ⇒ LinearFunction

Returns a new instance of LinearFunction



50
51
52
# File 'lib/linear_function.rb', line 50

def initialize(slope, y_int)
  		@slope, @y_intercept = slope, y_int
end

Instance Attribute Details

#slopeInteger (readonly) Also known as: m

Returns the x intercept of the graph

Returns:

  • (Integer)

    the x intercept of the graph



44
45
46
# File 'lib/linear_function.rb', line 44

def slope
  @slope
end

#y_interceptObject (readonly) Also known as: b

Returns the value of attribute y_intercept



47
48
49
# File 'lib/linear_function.rb', line 47

def y_intercept
  @y_intercept
end

Class Method Details

.borders=(bool) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
# File 'lib/linear_function.rb', line 38

def self.borders=(bool)
	raise ArgumentError, "Argument must be true or false" unless bool == true || bool == false
	@@borders = bool
end

.has_borders?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/linear_function.rb', line 23

def self.has_borders?
   		@@borders
end

.x_axisObject



5
6
7
# File 'lib/linear_function.rb', line 5

def self.x_axis
       	(0..(@@x_axis)).to_a
end

.x_axis=(num) ⇒ Object



9
10
11
12
# File 'lib/linear_function.rb', line 9

def self.x_axis=(num)
   		caa(num)
   		@@x_axis = num
end

.y_axisObject



14
15
16
# File 'lib/linear_function.rb', line 14

def self.y_axis
	(0..(@@y_axis)).to_a
end

.y_axis=(num) ⇒ Object



18
19
20
21
# File 'lib/linear_function.rb', line 18

def self.y_axis=(num)
	caa num
	@@y_axis = num
end

Instance Method Details

#domainArray<Integer>

Returns the values of the xy hash

Returns:

  • (Array<Integer>)

    the values of the xy hash



76
77
78
# File 'lib/linear_function.rb', line 76

def domain # @return [Array<Integer>] the values of the xy hash
	xy.values
end

#f(x) ⇒ Object



99
100
101
# File 'lib/linear_function.rb', line 99

def f(x)
	@slope * x + @y_intercept
end

#rangeArray<Integer>

Returns the keys of the xy hash

Returns:

  • (Array<Integer>)

    the keys of the xy hash



80
81
82
# File 'lib/linear_function.rb', line 80

def range # @return [Array<Integer>] the keys of the xy hash
	xy.keys
end

#raw_graphObject



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/linear_function.rb', line 26

def raw_graph
 		y = -1
	graph = Array.new(@@y_axis) do
		y += 1
		x = -1
		Array.new(@@x_axis) do
			x += 1
			format_pair x, y
		end
	end
end

#to_sString

Displays graph

Returns:

  • (String)

    the graph



88
89
90
91
92
93
94
95
96
97
# File 'lib/linear_function.rb', line 88

def to_s
	result = String.new
	for y_index in graph.reverse
		for x_index in y_index
			result << x_index
		end
		result << ?\n
	end
	return result.center 80
end

#x_interceptObject Also known as: x_int, zero, solution



54
55
56
# File 'lib/linear_function.rb', line 54

def x_intercept
	f(0)
end

#xyHash

Note:

There are no floats

The XY table

Returns:

  • (Hash)


66
67
68
69
70
71
72
73
74
# File 'lib/linear_function.rb', line 66

def xy
	table = Hash.new
	for y in LinearFunction.y_axis
		for x in LinearFunction.x_axis
			table[y] = x if f(x) == y
		end
	end
	return table
end