[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Newbe questions...

Chuck Brotman

6/18/2005 7:50:00 PM

In Ruby Is there a prefered (or otherwise elegant) way to do an inner &
outer loop with two variables?

eg in psedocode:

for i=1 to 3
{for j = 4 to 6
print i,j, i*j,\nl}

would print something like
1,4,4
1,5,5
1,6,6
2,4,8
2,5,10
2,6,12
3,4,12
3,5,15
3,6,18

Also,how would you generate the same output using the each iterator?


Thanks,
Chuck


16 Answers

James Gray

6/18/2005 8:02:00 PM

0

On Jun 18, 2005, at 2:50 PM, Chuck Brotman wrote:

> In Ruby Is there a prefered (or otherwise elegant) way to do an
> inner &
> outer loop with two variables?
>
> eg in psedocode:
>
> for i=1 to 3
> {for j = 4 to 6
> print i,j, i*j,\nl}

I don't personally have a problem with the way you show it right
there. I'm not aware of a significantly better way.

> would print something like
> 1,4,4
> 1,5,5
> 1,6,6
> 2,4,8
> 2,5,10
> 2,6,12
> 3,4,12
> 3,5,15
> 3,6,18
>
> Also,how would you generate the same output using the each iterator?

irb(main):007:0> (1..3).each do |i|
irb(main):008:1* (4..6).each do |j|
irb(main):009:2* puts "#{i} * #{j} = #{i * j}"
irb(main):010:2> end
irb(main):011:1> end
1 * 4 = 4
1 * 5 = 5
1 * 6 = 6
2 * 4 = 8
2 * 5 = 10
2 * 6 = 12
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
=> 1..3

Hope that helps.

James Edward Gray II


Sebastian Biallas

6/18/2005 8:03:00 PM

0

Chuck Brotman wrote:
> In Ruby Is there a prefered (or otherwise elegant) way to do an inner &
> outer loop with two variables?

Well, just nest a second loop into the outer loop...

>
> eg in psedocode:
>
> for i=1 to 3
> {for j = 4 to 6
> print i,j, i*j,\nl}
>
>
[..]

> Also,how would you generate the same output using the each iterator?

(1..3).each do |i|
(4..6).each do |j|
printf "%d, %d, %d\n", i, j, i*j
end
end

>
>
> Thanks,
> Chuck

Sebastian

Robert Klemme

6/18/2005 9:16:00 PM

0

Sebastian Biallas <groups.5.sepp@spamgourmet.com> wrote:
> Chuck Brotman wrote:
>> In Ruby Is there a prefered (or otherwise elegant) way to do an
>> inner & outer loop with two variables?
>
> Well, just nest a second loop into the outer loop...
>
>>
>> eg in psedocode:
>>
>> for i=1 to 3
>> {for j = 4 to 6
>> print i,j, i*j,\nl}
>>
>>
> [..]
>
>> Also,how would you generate the same output using the each iterator?
>
> (1..3).each do |i|
> (4..6).each do |j|
> printf "%d, %d, %d\n", i, j, i*j
> end
> end
>
>>
>>
>> Thanks,
>> Chuck
>
> Sebastian

Then there's for ..in:

for i in 1..3
begin
for j in 4..6
p [i,j,i*j]
end
end
end

HTH

robert

Ryan Leavengood

6/18/2005 10:00:00 PM

0

Robert Klemme wrote:
> Then there's for ..in:
>
> for i in 1..3
> begin
> for j in 4..6
> p [i,j,i*j]
> end
> end
> end

Or upto:

1.upto(3) do |i|
4.upto(6) do |j|
puts "#{i},#{j},#{i*j}"
end
end

Personally, I like that one.

Ryan


Chuck Brotman

6/19/2005 3:08:00 AM

0

Thank you all for your quick and helpful responses! In retrospect, it seems
obvious (ain't it always the way???) . I was able to get the for/in syntax
to work prior to asking the question, butr I couldn't get it to work with
"each" . I think I had the styntax wrong I used {} instead of do end, And I
must have screwed that up because I ened up in never-never-land runing on
freeRIDE's irb. I don't know if it was my or freeride!?).


FWIW I like this solution: best due to its succinctness and reradablility::

(1..3).each do |i|
(4..6).each do |j|
printf "%d, %d, %d\n", i, j, i*j
end
end

Thanks sebasation and edward (and robert and ryan, too).I guess just
couldn't figure out the exact syntax, myself.

Thanks again
Chuck


Stephan Kämper

6/19/2005 7:54:00 AM

0

Chuck Brotman wrote:
> Thank you all for your quick and helpful responses! In retrospect, it seems
> obvious (ain't it always the way???) . I was able to get the for/in syntax
> to work prior to asking the question, butr I couldn't get it to work with
> "each" . I think I had the styntax wrong I used {} instead of do end, And I

Both, pairs of curly braces and "do ... end", are valid ways of working
with 'each'. They mostly differ in precedence

> must have screwed that up because I ened up in never-never-land runing on
> freeRIDE's irb. I don't know if it was my or freeride!?).

If you show us the code (and the error message), we could find out what
went wrong.

> FWIW I like this solution: best due to its succinctness and reradablility::
>
> (1..3).each do |i|
> (4..6).each do |j|
> printf "%d, %d, %d\n", i, j, i*j
> end
> end

Note that this

(1..3).each{ |i|
(4..6).each{ |j|
printf "%d, %d, %d\n", i, j, i*j
}
}

is just as valid.


However, many (probably most) people prefer to use curly braces in one
liners like this

(4..6).each{ |j| printf "%d, %d, %d\n", i, j, i*j }

and "do ... end" for blocks with more than one line (like the example
above).


Happy Rubying

Stephan

Gene Tani

6/19/2005 2:16:00 PM

0

someone here mentioned they use {} or "do..end" based on whether the
block has side effects or not.

And the precedence comes into play when the method call preceding the
block has arguments that aren't in parenthese, then {} (the higher
precedence) will "pick off" the last arg from the method call (p 356
pickax)

Chuck Brotman

6/20/2005 6:00:00 PM

0

Stephan,

Thanks! I would *love* to show y'all my code and error messages, but I've
aready passed this particular hurdle and I'm off making newer and even
better bugs!

Rest assured: you'll be haring more questions from me before long...

Chuck
"Stephan Kämper" <Stephan.Kaemper@Schleswig-Holstein.de> wrote in message
news:42b52499$0$18648$14726298@news.sunsite.dk...
> Chuck Brotman wrote:
>> Thank you all for your quick and helpful responses! In retrospect, it
>> seems obvious (ain't it always the way???) . I was able to get the
>> for/in syntax to work prior to asking the question, butr I couldn't get
>> it to work with "each" . I think I had the styntax wrong I used {}
>> instead of do end, And I
>
> Both, pairs of curly braces and "do ... end", are valid ways of working
> with 'each'. They mostly differ in precedence
>
>> must have screwed that up because I ened up in never-never-land runing on
>> freeRIDE's irb. I don't know if it was my or freeride!?).
>
> If you show us the code (and the error message), we could find out what
> went wrong.
>
>> FWIW I like this solution: best due to its succinctness and
>> reradablility::
>>
>> (1..3).each do |i|
>> (4..6).each do |j|
>> printf "%d, %d, %d\n", i, j, i*j
>> end
>> end
>
> Note that this
>
> (1..3).each{ |i|
> (4..6).each{ |j|
> printf "%d, %d, %d\n", i, j, i*j
> }
> }
>
> is just as valid.
>
>
> However, many (probably most) people prefer to use curly braces in one
> liners like this
>
> (4..6).each{ |j| printf "%d, %d, %d\n", i, j, i*j }
>
> and "do ... end" for blocks with more than one line (like the example
> above).
>
>
> Happy Rubying
>
> Stephan


Chuck Brotman

6/20/2005 6:10:00 PM

0

some cinfusion on syntax...

Is it necessary (desirable) to end a single-line if/then statement with
"end.

The dopcumentation shows it when using else or multiline if..

Is the following okay?:

if condition then "the then code"

or should it be

if condition then "the then code" end ????

what about the use of curlybrackets "{}" how would that syntax go?


Chuck Brotman

6/20/2005 6:19:00 PM

0

When using the irb I sometimes end up in a state where hitting enter only
gives me a new prompt. I understand that I've (somehow) gotteninto the irb's
line continuation mode. It seems that anything I type at this point gets
swallowed up. Is there some way to get out of this, without exiting from
the irb altogether?