On Wednesday 11 June 2008, Jason Lillywhite wrote:
> I will settle with this:
>
> class MyArray
>   def count
>   a = [1,2,3]
>   b = [0.5,0.25]
>   num = a.length
>   i = 0
>   c = []
>     while i < num do
>       c << b.collect {|x| x * a[i]}
>       i += 1
>     end
>   puts c
>   end
> end
>
> number = MyArray.new
> number.count
>
>
> that way it does matter if I change the length of a.
>
> thanks again.

You don't need num. You can directly write:

while i < a.length
...

A much better alternative is:

a.each do |it|
  c << b.collect{|x| x * it}
end

a.each passes each element of a, in turn, as argument to the block. Each time 
the block is called, it calls b.collect, multiplying the elements of b for the 
current element of a.

An even more elegant way to do this is to use inject (see ri Enumerable#inject 
for an explanation of how it works):

a = [1,2,3]
b = [0.5,0.25]
c = a.inject([]) do |res, i|
  res << b.collect{|x| x*i}
end

Stefano