J
Jason Lillywhite
How do you take a piece of a method and use it in another? Here is my
simple example:
class MyArray
def counter
result = []
i = 0
a = 1
b = 0
while i <= 5 do
a = a + 1
b = b +1.5
result << b.to_s + "\t" + a.to_s
i += 1
end
puts result
end
def counter2
counter
end
end
test = MyArray.new
puts test.counter2
I want counter2 method to take the array "b" and do something with it,
like this:
b.inject {|sum, element| sum+element}
I just can't figure out how to transfer b from one method to another. Is
there a clean way to do this?
Thank you!
simple example:
class MyArray
def counter
result = []
i = 0
a = 1
b = 0
while i <= 5 do
a = a + 1
b = b +1.5
result << b.to_s + "\t" + a.to_s
i += 1
end
puts result
end
def counter2
counter
end
end
test = MyArray.new
puts test.counter2
I want counter2 method to take the array "b" and do something with it,
like this:
b.inject {|sum, element| sum+element}
I just can't figure out how to transfer b from one method to another. Is
there a clean way to do this?
Thank you!