T
Tim L
Hi:
This is a newbie question.
In the code below I'm developing methods to generate various mathematical
sequences.
I'm really happy that I can pass a block of code to define the rule to
generate the next number in a sequence, e.g. the Fibonacci sequence. But I
also need a rule to say when to stop, and I was wondering whether I could
also pass that as a block. I can't see how, although I can define it as a
Proc.
Tim L
EPS = 10 ** -12 # i.e. a very small number to test for convergence
#uses (1) push, last array methods
# uses negative indices to get most recent numbers put in list
#Fibonacci
seq = [1, 1]
9.times do |i|
seq.push(seq[-1] + seq[-2])
end
puts seq.join(", ")
#Root N
n = 1.5 # make sure a float
seq = [0,1]
while seq[-1] != seq[-2]
seq.push(seq[-1] + (n - seq[-1]**2 ) / (seq[-1] * 2) )
end
puts seq.last
puts seq.length
puts n**(0.5)
seq = [0,1]
while (seq[-1] - seq[-2]).abs >EPS
seq.push(seq[-1] + (n - seq[-1]**2 ) / (seq[-1] * 2) )
end
puts seq.last
puts seq.length
puts n**(0.5)
fibo = Proc.new do |arr| #capital letter in Proc matters!
arr.push(arr[-1] + arr[-2])
end
seq = [1,1]
9.times do
fibo.call(seq)
end
puts seq.join(", ")
def buildSeq(arr, &pr) # '&' indicates that the argument here is going to be
a block
9.times do
pr.call(arr)
end
end
seq = [1,1]
buildSeq(seq) {|arr| arr.push(arr[-1] + arr[-2]) }
puts seq.join(", ")
myTest = Proc.new do |arr|
(arr[-1] - arr[-2]).abs > EPS
end
def buildSeqTest(arr, test, &pr) # test is a proc. Why can't I pass two
blocks to a method?
while test.call(arr)
pr.call(arr)
end
end
n = 17.0
seq = [1,2]
buildSeqTest(seq, myTest) {|arr| arr.push(arr[-1] + (n - arr[-1]**2 ) /
(arr[-1] * 2) )}
puts seq.last
This is a newbie question.
In the code below I'm developing methods to generate various mathematical
sequences.
I'm really happy that I can pass a block of code to define the rule to
generate the next number in a sequence, e.g. the Fibonacci sequence. But I
also need a rule to say when to stop, and I was wondering whether I could
also pass that as a block. I can't see how, although I can define it as a
Proc.
Tim L
EPS = 10 ** -12 # i.e. a very small number to test for convergence
#uses (1) push, last array methods
# uses negative indices to get most recent numbers put in list
#Fibonacci
seq = [1, 1]
9.times do |i|
seq.push(seq[-1] + seq[-2])
end
puts seq.join(", ")
#Root N
n = 1.5 # make sure a float
seq = [0,1]
while seq[-1] != seq[-2]
seq.push(seq[-1] + (n - seq[-1]**2 ) / (seq[-1] * 2) )
end
puts seq.last
puts seq.length
puts n**(0.5)
seq = [0,1]
while (seq[-1] - seq[-2]).abs >EPS
seq.push(seq[-1] + (n - seq[-1]**2 ) / (seq[-1] * 2) )
end
puts seq.last
puts seq.length
puts n**(0.5)
fibo = Proc.new do |arr| #capital letter in Proc matters!
arr.push(arr[-1] + arr[-2])
end
seq = [1,1]
9.times do
fibo.call(seq)
end
puts seq.join(", ")
def buildSeq(arr, &pr) # '&' indicates that the argument here is going to be
a block
9.times do
pr.call(arr)
end
end
seq = [1,1]
buildSeq(seq) {|arr| arr.push(arr[-1] + arr[-2]) }
puts seq.join(", ")
myTest = Proc.new do |arr|
(arr[-1] - arr[-2]).abs > EPS
end
def buildSeqTest(arr, test, &pr) # test is a proc. Why can't I pass two
blocks to a method?
while test.call(arr)
pr.call(arr)
end
end
n = 17.0
seq = [1,2]
buildSeqTest(seq, myTest) {|arr| arr.push(arr[-1] + (n - arr[-1]**2 ) /
(arr[-1] * 2) )}
puts seq.last