# Two Problems From a Newbie

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.

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.

Any ideas?

Thanks a lot guys,
danielj

just to be perfectly clear, i mean that i don’t understand why it is 6
and 36

so basically, i don’t understand what the method does on the “inside”

On Jun 21, 2007, at 1:50 AM, danielj wrote:

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:

``` def foo 10.times do |n| a = [n, n * n] return a if n > 5 end end num, square = foo puts num, square ```

But consider the following:

``` def foo 10.times { |n| return [n, n * n] if n > 5 } end num, square = foo puts num, square ```

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.

Regards, Morton

Quite simple: The times methods iterates ten times from 0 to 9.
But with the “return”, you can eventually break the iterations before
“9” depending on the result of the condition “if num > 5”.
In your case, the condition becomes true as soon as num = 6. So your
method returns 6 and 6*6=36.

On 21 Jun 2007, at 14:55, danielj wrote:

just to be perfectly clear, i mean that i don’t understand why it is 6
and 36

so basically, i don’t understand what the method does on the “inside”

It would be easier to explain if you said what you are expecting to
happen!

end
The 10.times block (do/end) is called with the integer ‘num’. The
first time it is called with 0 and each subsequent time this number
is increased by 1. Each time through the loop you calculate the
square of the number. You return from the method when the number is
greater than 5 (i.e. when it is 6). Your method therefore returns 6
and 36 (the square of 6).

Hope that helps.

Alex G.

Bioinformatics Center
Kyoto University

On Jun 21, 1:48 am, danielj [email protected] wrote:

def mtdarry

10.times do |num|

``````square = num * num

return num, square if num > 5
``````

Don’t know if this pertains to your problem, since you haven’t said
explicitly what you’re trying to do, but it looks like you want the
square of all numbers x where 5 < x < 10? If that’s the case, there
are two problems. One, as was mentioned, is that .times starts at 0.
The other is that the keyword “return” will exit the function. It will
pass num, square to the source of the function call, and then it the
function is done being evaluated.

You can solve this by either building an array of value/square pairs
or moving the iteration up a level, so that the method gets called 10
times with a new value each time.

danielj wrote:

I can’t figure out why this outputs they way it does? I know it is
simple but I just don’t get it.

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.

Any ideas?

… I guess you are on windows.

Ignore the cmd.window that pops up and type directly into the
SciTE-output-pane (the one being opened when pressing F8).

Cheers

Stefan