Newbe questions

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

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

···

On Jun 18, 2005, at 2:50 PM, Chuck Brotman 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

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?

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?

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

···

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

Thanks,
Chuck

Sebastian

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

···

On Jun 20, 2005, at 1:10 PM, Chuck Brotman wrote:

Hmm, could you show us some examples?

James Edward Gray II

···

On Jun 20, 2005, at 1:30 PM, Chuck Brotman wrote:

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?

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

James,
  Yes I think that helps!
Thanks,
Chuck
"James Edward Gray II" <james@grayproductions.net> wrote in message
news:86987EF9-CC8B-4BCE-9DFB-4ADEC4D005ED@grayproductions.net...

···

On Jun 20, 2005, at 1:10 PM, Chuck Brotman wrote:

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

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

He means:

if 1 == 1
puts 'yes'

?>

Now two things help here:

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

- pressing CTRL-C

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

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

···

James Edward Gray II <james@grayproductions.net> wrote:

On Jun 20, 2005, at 1:30 PM, 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
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

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

···

On Jun 20, 2005, at 1:55 PM, Robert Klemme wrote:

He means:

if 1 == 1
puts 'yes'

?>

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!!

"Robert Klemme" <bob.news@gmx.net> wrote in message
news:3hohmbFi1921U1@individual.net...

···

James Edward Gray II <james@grayproductions.net> wrote:

On Jun 20, 2005, at 1:30 PM, Chuck Brotman wrote:

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

He means:

if 1 == 1
puts 'yes'

?>
?>
?>
?>
?>

Now two things help here:

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

- pressing CTRL-C

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

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

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

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

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)

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