[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Two Problems From a Newbie

danielj

6/21/2007 5:49:00 AM

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

6 Answers

danielj

6/21/2007 5:53:00 AM

0

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"

Morton Goldberg

6/21/2007 7:13:00 AM

0


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

come

6/21/2007 7:24:00 AM

0

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 juin, 07:53, danielj <dani...@sleepingindian.org> 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"


Alex Gutteridge

6/21/2007 8:00:00 AM

0

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!

> def mtdarry
>
> 10.times do |num|
>
> square = num * num
>
> return num, square if num > 5
>
> end
>
> 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 Gutteridge

Bioinformatics Center
Kyoto University



Stefan Mahlitz

6/21/2007 6:45:00 PM

0

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

Kaldrenon

6/21/2007 9:00:00 PM

0

On Jun 21, 1:48 am, danielj <dani...@sleepingindian.org> 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.