Class: Linear1::ArithmeticSequence

Inherits:
Function
  • Object
show all
Defined in:
lib/linear1/arithmetic_sequence.rb

Instance Attribute Summary collapse

Attributes inherited from Function

#power

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Function

#direct_variation?, display_num, #execute, #to_direct_variation, #to_function, #to_slope_intercept, #x_intercept

Constructor Details

#initialize(*a) ⇒ ArithmeticSequence

Returns a new instance of ArithmeticSequence



12
13
14
15
16
17
18
19
20
# File 'lib/linear1/arithmetic_sequence.rb', line 12

def initialize *a
	i = 0
	while i < a.length - 2 
		raise ArgumentError, "Elements must have a common difference" unless a[i+1] - a[i] == a[i+2] - a[i+1]
		i += 1
	end
	@common_difference, @a = display_num(a[1]) - display_num(a[0]), a
	super @common_difference, @a[0]
end

Instance Attribute Details

#aObject (readonly) Also known as: to_a

Returns the value of attribute a



7
8
9
# File 'lib/linear1/arithmetic_sequence.rb', line 7

def a
  @a
end

#common_differenceObject (readonly) Also known as: d

Returns the value of attribute common_difference



7
8
9
# File 'lib/linear1/arithmetic_sequence.rb', line 7

def common_difference
  @common_difference
end

Class Method Details

.find(index) ⇒ Object



4
5
6
# File 'lib/linear1/arithmetic_sequence.rb', line 4

def self.find index
	new *(ARGV[index..(ARGV.length - 1)])
end

Instance Method Details

#to_sObject

Modified to count from zero instead of one



21
22
23
# File 'lib/linear1/arithmetic_sequence.rb', line 21

def to_s # Modified to count from zero instead of one
	"a[n] = #{a[0]} + #{idx d}n"
end