Class: LinearGraph
- Inherits:
-
Object
- Object
- LinearGraph
- Defined in:
- lib/linear_graph.rb
Instance Attribute Summary collapse
-
#slope ⇒ Integer
(also: #m)
readonly
The x intercept of the graph.
-
#y_intercept ⇒ Object
(also: #b)
readonly
Returns the value of attribute y_intercept.
Class Method Summary collapse
- .borders=(bool) ⇒ Object
- .has_borders? ⇒ Boolean
- .x_axis ⇒ Object
- .x_axis=(num) ⇒ Object
- .y_axis ⇒ Object
- .y_axis=(num) ⇒ Object
Instance Method Summary collapse
-
#domain ⇒ Array<Integer>
The values of the xy hash.
- #f(x) ⇒ Object
- #graph ⇒ Object
-
#initialize(slope, y_int) ⇒ LinearGraph
constructor
A new instance of LinearGraph.
-
#range ⇒ Array<Integer>
The keys of the xy hash.
-
#to_s ⇒ String
Displays graph.
- #x_intercept ⇒ Object (also: #x_int, #zero, #solution)
-
#xy ⇒ Hash
The XY table.
Constructor Details
#initialize(slope, y_int) ⇒ LinearGraph
50 51 52 |
# File 'lib/linear_graph.rb', line 50 def initialize(slope, y_int) @slope, @y_intercept = slope, y_int end |
Instance Attribute Details
#slope ⇒ Integer (readonly) Also known as: m
44 45 46 |
# File 'lib/linear_graph.rb', line 44 def slope @slope end |
#y_intercept ⇒ Object (readonly) Also known as: b
Returns the value of attribute y_intercept.
47 48 49 |
# File 'lib/linear_graph.rb', line 47 def y_intercept @y_intercept end |
Class Method Details
.borders=(bool) ⇒ Object
38 39 40 41 |
# File 'lib/linear_graph.rb', line 38 def self.borders=(bool) raise ArgumentError, "Argument must be true or false" unless bool == true | false @@borders = bool end |
.has_borders? ⇒ Boolean
23 24 25 |
# File 'lib/linear_graph.rb', line 23 def self.has_borders? @@borders end |
.x_axis ⇒ Object
5 6 7 |
# File 'lib/linear_graph.rb', line 5 def self.x_axis (0..(@@x_axis)).to_a end |
.x_axis=(num) ⇒ Object
9 10 11 12 |
# File 'lib/linear_graph.rb', line 9 def self.x_axis=(num) caa(num) @@x_axis = num end |
.y_axis ⇒ Object
14 15 16 |
# File 'lib/linear_graph.rb', line 14 def self.y_axis (0..(@@y_axis)).to_a end |
.y_axis=(num) ⇒ Object
18 19 20 21 |
# File 'lib/linear_graph.rb', line 18 def self.y_axis=(num) caa num @@y_axis = num end |
Instance Method Details
#domain ⇒ Array<Integer>
76 77 78 |
# File 'lib/linear_graph.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_graph.rb', line 99 def f(x) @slope * x + @y_intercept end |
#graph ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/linear_graph.rb', line 26 def 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 |
#range ⇒ Array<Integer>
80 81 82 |
# File 'lib/linear_graph.rb', line 80 def range # @return [Array<Integer>] the keys of the xy hash xy.keys end |
#to_s ⇒ String
Displays graph
88 89 90 91 92 93 94 95 96 97 |
# File 'lib/linear_graph.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_intercept ⇒ Object Also known as: x_int, zero, solution
54 55 56 |
# File 'lib/linear_graph.rb', line 54 def x_intercept f(0) end |
#xy ⇒ Hash
Note:
There are no floats
The XY table
66 67 68 69 70 71 72 73 74 |
# File 'lib/linear_graph.rb', line 66 def xy table = Hash.new for y in LinearGraph.y_axis for x in LinearGraph.x_axis table[y] = x if f(x) == y end end return table end |