Two Problems From a Newbie

D

danielj

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
 
D

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"
 
M

Morton Goldberg

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
 
C

come

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.
 
A

Alex Gutteridge

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
 
S

Stefan Mahlitz

danielj said:
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
 
K

Kaldrenon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top