Morton Goldberg
6/21/2007 7:13:00 AM
On Jun 21, 2007, at 1:50 AM, danielj wrote:
> def mtdarry
>
> 10.times do |num|
>
> square = num * num
>
> return num, square if num > 5
>
> end
>
> end
>
> num, square = mtdarry
>
> puts num
> puts square
>
> I can't figure out why this outputs they way it does? I know it is
> simple but I just don't get it.
The above is equivalent to:
<code>
def foo
10.times do |n|
a = [n, n * n]
return a if n > 5
end
end
num, square = foo
puts num, square
</code>
But consider the following:
<code>
def foo
10.times { |n| return [n, n * n] if n > 5 }
end
num, square = foo
puts num, square
</code>
This is better because it only computes the array for n = 6, not for
0..6.
> Secondly, I have just started using the SCiTE editor instead of
> metapad and it is great! However, when I try to get a string it won't
> let me type into the window that pops up.
Can't help you here. Don't know anything about the SCiTE editor.
Regards, Morton