For Loop question

M

Mark Mr

ok basically i cant quite figure out how to do a for loop i want in
ruby. Here's what i want to do in C++, with some ruby mixed in

for (i = 0; @question_array.nil?; i++)
{

code here....

}

it's supposed to run until it finds an element of the array that isnt
blank and then stop. anyone know how to make a loop like this in ruby?
thanks :)
 
J

Jason Roelofs

Assuming you want to know where in the array said item is, this will do:

@question_array.each_with_index do |part, i|
if part.nil?
count = i
break
end
end

otherwise the whole inner if can be simplified to "break if part.nil?"

Jason
 
J

Jason Roelofs

Crap, I mean

if !part.nil?

Jason

Assuming you want to know where in the array said item is, this will do:

@question_array.each_with_index do |part, i|
if part.nil?
count = i
break
end
end

otherwise the whole inner if can be simplified to "break if part.nil?"

Jason


ok basically i cant quite figure out how to do a for loop i want in
ruby. Here's what i want to do in C++, with some ruby mixed in

for (i = 0; @question_array.nil?; i++)
{

code here....

}

it's supposed to run until it finds an element of the array that isnt
blank and then stop. anyone know how to make a loop like this in ruby?
thanks :)
 
T

Thomas Preymesser

ok basically i cant quite figure out how to do a for loop i want in
ruby. Here's what i want to do in C++, with some ruby mixed in

for (i =3D 0; @question_array.nil?; i++)
{

code here....

}

it's supposed to run until it finds an element of the array that isnt
blank and then stop. anyone know how to make a loop like this in ruby?
thanks :)

i =3D 0
while @question_array.nil? do
code here....
i +=3D 1
end

this is the semantic aequivalent of the C/C++ loop above.


--=20
Thomas Preymesser
(e-mail address removed)
(e-mail address removed)
B=FCro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://thopre.wordpress.com/
http://www.thopre.com/
 
D

Dick Davies

question_array.each { |element|
next if element.nil
element.do_something
}

will call do_something() on every element in the array that isn't nil.

If you just want to do something with the first non-nil element, you could do

non_nil = element.find { |e| !e.nil? }
non_nil.do_something

. Here's what i want to do in C++, with some ruby mixed in

for (i = 0; @question_array.nil?; i++)
{
code here....
}

it's supposed to run until it finds an element of the array that isnt
blank and then stop. anyone know how to make a loop like this in ruby?
thanks :)
 
7

7stud --

Mark said:
ok basically i cant quite figure out how to do a for loop i want in
ruby. Here's what i want to do in C++, with some ruby mixed in

for (i = 0; @question_array.nil?; i++)
{

code here....

}

it's supposed to run until it finds an element of the array that isnt
blank and then stop. anyone know how to make a loop like this in ruby?
thanks :)


"isnt blank" != nil
 
S

Siep Korteling

7stud said:
Whoops. That should be:

"blank" != nil

But what is "blank" in an array? You can't do a=[nil,,1,2,3] .
Anyway, this is also possible:

a=[nil,nil,nil,2,3,nil,4]
a.detect{|n|n}
=> 2
 
J

Jari Williamsson

Dick said:
question_array.each { |element|
next if element.nil
element.do_something
}

will call do_something() on every element in the array that isn't nil.

Or a version that will break after the first non-nil:

question_array.each { |element|
next if element.nil
break element.do_something
}

If you just want to do something with the first non-nil element, you could do

non_nil = element.find { |e| !e.nil? }
non_nil.do_something

To protect from arrays with all-nils:

non_nil = element.find { |e| !e.nil? }
non_nil.do_something if non_nil


Best regards,

Jari Williamsson
 
T

tho_mica_l

it's supposed to run until it finds an element of the array that isnt
blank and then stop.

And then do what? Do you need the index? In this case you can use the
index method.

a = [1,2,nil,3]

a.index(nil)
# => 2

or do you want the elements before that? Or ...

Anyway, another solution close in resemblance to yours could be:

for e in a
break if e.nil?
do_something e
end

HTH,
Thomas.
 
M

Mark Mr

thanks to everyone who posted. This one worked for what i was doing

i = 0
while @question_array.nil? do
code here....
i += 1
end

although i was hoping i wouldn't have to declare the i=0 beforehand just
from a style standpoint but it works fine so i guess that's all that
matters. Thanks again :)
 
R

Robert Klemme

2008/1/30 said:
thanks to everyone who posted. This one worked for what i was doing

i = 0
while @question_array.nil? do
code here....


What code? The question is what are you trying to achieve here? This
has not become clear from your postings.
i += 1
end

although i was hoping i wouldn't have to declare the i=0 beforehand just
from a style standpoint but it works fine so i guess that's all that
matters. Thanks again :)

If you are doing a kind of initialization you could do this

@questions.map! do |q|
q or begin
# init code, here just a constant value
1
end
end

Kind regards

robert
 

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
474,276
Messages
2,571,384
Members
48,073
Latest member
ImogenePal

Latest Threads

Top