Class: Linear1::Function
- Inherits:
-
Object
show all
- Defined in:
- lib/linear1/function.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(slope = 1, y_intercept = 0, power = 1) ⇒ Function
Returns a new instance of Function.
12
13
14
|
# File 'lib/linear1/function.rb', line 12
def initialize(slope=1, y_intercept=0, power=1)
@slope, @y_intercept, @power = display_num(slope), display_num(y_intercept), display_num(power)
end
|
Instance Attribute Details
#power ⇒ Object
Returns the value of attribute power.
6
7
8
|
# File 'lib/linear1/function.rb', line 6
def power
@power
end
|
#slope ⇒ Object
Returns the value of attribute slope.
6
7
8
|
# File 'lib/linear1/function.rb', line 6
def slope
@slope
end
|
#y_intercept ⇒ Object
Returns the value of attribute y_intercept.
6
7
8
|
# File 'lib/linear1/function.rb', line 6
def y_intercept
@y_intercept
end
|
Class Method Details
9
10
11
|
# File 'lib/linear1/function.rb', line 9
def self.find i1
Function.new ARGV[i1], ARGV[i1 + 1], ARGV[i1 + 2]
end
|
Instance Method Details
#direct_variation? ⇒ Boolean
Also known as:
dv?
31
32
33
|
# File 'lib/linear1/function.rb', line 31
def direct_variation?
y_intercept.zero? and power == 1
end
|
#execute(x) ⇒ Integer, Float
Also known as:
f
17
18
19
|
# File 'lib/linear1/function.rb', line 17
def execute x
slope * display_num(x) ** power + y_intercept
end
|
#to_direct_variation ⇒ DirectVariation
Also known as:
to_dv
37
38
39
40
41
42
|
# File 'lib/linear1/function.rb', line 37
def to_direct_variation
if direct_variation?
DirectVariation.new slope
else raise TypeError, "Unable to convert to DirectVariation"
end
end
|
#to_s ⇒ String
28
29
30
|
# File 'lib/linear1/function.rb', line 28
def to_s
"f(x) = #{idx display_num slope}x#{power_string unless power == 1}#{" + #{display_num @y_intercept}" unless direct_variation?}"
end
|
#to_slope_intercept ⇒ Object
43
44
45
46
|
# File 'lib/linear1/function.rb', line 43
def to_slope_intercept
raise TypeError, "power must be 1" unless power == 1
SlopeIntercept.new slope, y_intercept
end
|
#x_intercept ⇒ Object
Also known as:
zero, solution, root
21
22
23
|
# File 'lib/linear1/function.rb', line 21
def x_intercept
f(0)
end
|