While statements in ruby

Hi guys, I have a probably simple question. I dont know how to do
iteration loops in ruby that reference more than one item of an array or
more than one array. Here’s an example of what I would do in php. Can
anyone convert this to ruby? Thanks :slight_smile:

while ($i=0; $i < $response_table.length; $i += 2) {

$response_table[$i] $response_table[$i + 1]

}

or something like this would work too if i made an array for questions
and a separate one for answers

while ($i=0; $i < array_count; $i++) {

$question_table[$i] $answer_table[$i]

}

Mark Mr wrote:

Hi guys, I have a probably simple question. I dont know how to do
iteration loops in ruby that reference more than one item of an array or
more than one array. Here’s an example of what I would do in php. Can
anyone convert this to ruby? Thanks :slight_smile:

while ($i=0; $i < $response_table.length; $i += 2) {

$response_table[$i] $response_table[$i + 1]

}

or something like this would work too if i made an array for questions
and a separate one for answers

while ($i=0; $i < array_count; $i++) {

$question_table[$i] $answer_table[$i]

}

To make a similar loop to yours above you can use the times method.
Like:

response_table.size.times do |i|
response_table[i]
response_table[i+1]
end

question_table.size.times do |i|
question_table[i]
answer_table[i]
end

Mark Mr wrote:

Hi guys, I have a probably simple question. I dont know how to do
iteration loops in ruby that reference more than one item of an array or
more than one array. Here’s an example of what I would do in php. Can
anyone convert this to ruby? Thanks :slight_smile:

Many answers to your question are here:
http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/a1fd3829ff41e482

Suggest you stop using ruby-forum until it stops hiding the answers
being given.

Hm how did my question get there? I’m a little confused :stuck_out_tongue: Anyway thanks
to the first poster, that method worked great. I tried each_with_index
before i posted this and for some reason it was giving me strange
numbers instead of the corresponding values.

Mark Mr wrote:

Hm how did my question get there? I’m a little confused :stuck_out_tongue: Anyway thanks
to the first poster, that method worked great. I tried each_with_index
before i posted this and for some reason it was giving me strange
numbers instead of the corresponding values.

When you post to ruby-forum, you will see the following right above
where you type in your message:

“This forum is connected to a mailing list that is read by thousands of
people. Before you post, please use the FAQ, the Ruby documentation and
Google […]”

ruby-talk is the main mailing list for Ruby-related discussion. A
gateway transfers information between that mailing list and the
comp.lang.ruby newsgroup.

Ruby-forum is hooked up to one or the other. (I assume the mailing
list.) For some reason, it recently has been sending messages to the
mailing list, but not posting responses as answers. So - until it’s
fixed, you can sign up to the ruby-talk mailing list:
http://www2.ruby-lang.org/en/20020104.html
or you can read and post via a newsreader, or (like I do) use Google
Groups to access comp.lang.ruby:
http://groups.google.com/group/comp.lang.ruby/

On 11.12.2007 18:25, Mark Mr wrote:

}
I am amazed nobody mentioned each_slice: with “enumerator” you can do:

irb(main):006:0> (1…6).each_slice(2) {|a,b| print a,",",b,"\n"}
1,2
3,4
5,6
=> nil

or something like this would work too if i made an array for questions
and a separate one for answers

while ($i=0; $i < array_count; $i++) {

$question_table[$i] $answer_table[$i]

}

questions.size.times do |i|
puts questions[i], answers[i]
end

Kind regards

robert

oh ok well thanks for the tip. I tried replying via the google group
page but it didnt show up so I’ll post it here instead.

In response to this reply:

If you are really just working with two arrays that map one-to-one and
onto each other, why not use one array? An array of hashes perhaps.
Like:

q_and_as = [ {:question => ‘Is it blue?’, :answer => ‘Yes’},
{:question => ‘Is it round?’, :answer => ‘No’} ]

Then use Array’s each method:

q_and_as.each do |query|
puts “

#{query[:question]}”
puts “#{query[:answer]}”
end

HTH,
Chris

I wanted to do this too but I didnt know how in Ruby. How would I add to
an array like this using variables in a loop? If I knew that I could
make it alot easier. Thanks :slight_smile:

On Dec 11, 10:25 am, Mark Mr [email protected] wrote:

}


Posted viahttp://www.ruby-forum.com/.

If you are really just working with two arrays that map one-to-one and
onto each other, why not use one array? An array of hashes perhaps.
Like:

q_and_as = [ {:question => ‘Is it blue?’, :answer => ‘Yes’},
{:question => ‘Is it round?’, :answer => ‘No’} ]

Then use Array’s each method:

q_and_as.each do |query|
puts “#{query[:question]}”
puts “#{query[:answer]}”
end

HTH,
Chris

On Dec 11, 10:25 am, Mark Mr [email protected] wrote:

while ($i=0; $i < $response_table.length; $i += 2) {

$response_table[$i] $response_table[$i + 1]

}

Oh, and some non-rubyish answers:

irb(main):019:0> i=0
irb(main):020:0> while i<a.length
irb(main):021:1> print a[i], a[i+1], “\n”
irb(main):022:1> i += 1
irb(main):023:1> end
ab
bc
cd
de
ef
fnil

irb(main):024:0> 0.step( a.length-1, 2 ){ |i|
irb(main):025:1* print a[i], a[i+1], “\n”
irb(main):026:1> }
ab
cd
ef

On Dec 11, 11:41 am, Chris S. [email protected] wrote:

$question_table[$i]

HTH,
Chris

Yeah I wanted to do this as well but didn’t know how in ruby :slight_smile: Could
you tell me how you’d add to an array like this using variables inside
a loop? Like if you didnt manually enter the values.

On Dec 11, 10:25 am, Mark Mr [email protected] wrote:

}

or something like this would work too if i made an array for questions
and a separate one for answers

irb(main):001:0> a = %w| a b c d e f |
=> [“a”, “b”, “c”, “d”, “e”, “f”]

irb(main):005:0> a.each_with_index{ |val, i|
irb(main):006:1* print val, a[i+1], “\n”
irb(main):007:1> }
ab
bc
cd
de
ef
fnil

irb(main):009:0> require ‘enumerator’
irb(main):011:0> a.each_cons(2){ |x,y|
irb(main):012:1* print x, y, “\n”
irb(main):013:1> }
ab
bc
cd
de
ef

irb(main):014:0> b = %w| 1 2 3 4 5 6 |
=> [“1”, “2”, “3”, “4”, “5”, “6”]

irb(main):015:0> a.zip(b)
=> [[“a”, “1”], [“b”, “2”], [“c”, “3”], [“d”, “4”], [“e”, “5”],
[“f”, “6”]]

irb(main):016:0> a.zip(b).each{ |alpha, num|
irb(main):017:1* print alpha, num, “\n”
irb(main):018:1> }
a1
b2
c3
d4
e5
f6

On Tue, 11 Dec 2007 23:00:52 +0100, Robert K. wrote:

}

I am amazed nobody mentioned each_slice: with “enumerator” you can do:

irb(main):006:0> (1…6).each_slice(2) {|a,b| print a,",",b,"\n"} 1,2
3,4
5,6
=> nil

Because someone did mention #each_cons
In reality, you’re right and they (and most other people, myself
included) didn’t read closely enough to see that the loop does
i+=2, not i+=1.

–Ken

On Dec 13, 8:21 am, Ken B. [email protected] wrote:

On Tue, 11 Dec 2007 23:00:52 +0100, Robert K. wrote:

I am amazed nobody mentioned each_slice: with “enumerator” you can do:
[…]
Because someone did mention #each_cons
In reality, you’re right and they (and most other people, myself
included) didn’t read closely enough to see that the loop does
i+=2, not i+=1.

Exactly why I didn’t mention it. :slight_smile: Glad Robert was there to pick up
the slack.