Newbe questions...

C

Chuck Brotman

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
 
J

James Edward Gray II

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
 
S

Sebastian Biallas

Chuck said:
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
 
R

Robert Klemme

Sebastian Biallas said:
Chuck said:
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
 
R

Ryan Leavengood

Robert said:
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
 
C

Chuck Brotman

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
 
S

Stephan Kämper

Chuck said:
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
 
G

Gene Tani

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)
 
C

Chuck Brotman

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
 
C

Chuck Brotman

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?
 
C

Chuck Brotman

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?
 
J

James Edward Gray II

some cinfusion on syntax...

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

Usually, but read on.
The dopcumentation shows it when using else or multiline if..

Is the following okay?:

if condition then "the then code"

No, that's not okay. However, Ruby allows statement modifiers, so we
can reverse that:

do_this() if condition?

That is allow and no end is needed.
or should it be

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

The above is fine. You can also replace then with it's symbol
shortcut, in recent versions of Ruby:

if condition? : do_this() end
what about the use of curlybrackets "{}" how would that syntax go?

No braces for if's, use end instead.

Hope that helps.

James Edward Gray II
 
C

Chuck Brotman

James,
Yes I think that helps!
Thanks,
Chuck
James Edward Gray II said:
Usually, but read on.


No, that's not okay. However, Ruby allows statement modifiers, so we can
reverse that:

do_this() if condition?

That is allow and no end is needed.


The above is fine. You can also replace then with it's symbol shortcut,
in recent versions of Ruby:

if condition? : do_this() end


No braces for if's, use end instead.

Hope that helps.

James Edward Gray II
 
J

James Edward Gray II

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?

Hmm, could you show us some examples?

James Edward Gray II
 
R

Robert Klemme

James Edward Gray II said:
Hmm, could you show us some examples?

James Edward Gray II

He means:
?>
?>
?>
?>
?>

Now two things help here:

- entering "end<cr>" until the normal prompt comes back.

- pressing CTRL-C

Note, how "exit<cr>" does *not* help. :)

Chuck, with your question frequency you might want to join #ruby-lang on
freenode - a lot of Ruby folks hang out there and you usually get immediate
feedback (plus some weired comments, if someone is in the mood... :))

Kind regards

robert
 
J

James Edward Gray II

He means:


?>
?>
?>
?>
?>

This is why I asked for examples.

What is happening above is that you've entered a partially complete
Ruby construct. The if isn't done until it sees an end. That's why
irb is waiting; it wants you to complete the thought.

I think it's important to understand that, because typing end isn't
always the answer.

James Edward Gray II
 
C

Chuck Brotman

Robert,
You've got it! Yes that's the problem I'm having!

James,
Here are the actual results showing "the problem" andthat end<cr> does
the trick:



c:\ruby182-15\freeride>irb
irb(main):001:0> if 1==1
irb(main):002:1> puts "yes"
irb(main):003:1>
irb(main):004:1*
irb(main):005:1*
irb(main):006:1*
irb(main):007:1*
irb(main):008:1* end
yes
=> nil
irb(main):009:0>
=> nil


Thanks again!!
 

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

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,484
Latest member
JackRichard

Latest Threads

Top