# Two Problems From a Newbie

**URL:** <https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393>\
**Category:** Ruby\
**Created:** [June 21, 2007, 7:52am UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393 "2007-06-21T07:52:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [June 21, 2007, 7:52am UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393/1 "2007-06-21T07:52:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [June 21, 2007, 7:56am UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393/2 "2007-06-21T07:56:23Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [June 21, 2007, 9:14am UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393/3 "2007-06-21T09:14:28Z")

</div>

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.

Can’t help you here. Don’t know anything about the SCiTE editor.

Regards, Morton

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [June 21, 2007, 9:27am UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393/4 "2007-06-21T09:27:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [June 21, 2007, 10:00am UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393/5 "2007-06-21T10:00:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [June 21, 2007, 11:07pm UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393/6 "2007-06-21T23:07:46Z")

</div>

On Jun 21, 1:48 am, danielj [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![danielj](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e56c9b/32.png) [@danielj](https://www.ruby-forum.com/u/danielj)\
**Post date:** [June 21, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/two-problems-from-a-newbie/107393/7 "2007-06-21T20:48:26Z")

</div>

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.

Others have answered your first question …

> 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
