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[i].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 
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
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
On 29/01/2008, Mark Mr [email protected] wrote:
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
Posted via http://www.ruby-forum.com/.
i = 0
while @question_array[i].nil? do
code here…
i += 1
end
this is the semantic aequivalent of the C/C++ loop above.
–
Thomas P.
[email protected]
[email protected]
Büro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06
http://www.thopre.com/
7stud – wrote:
7stud – wrote:
“isnt blank” != nil
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
7stud – wrote:
7stud – wrote:
“isnt blank” != nil
Whoops. That should be:
“blank” != nil
Double whoops. I guess “blank” does equal nil.
Dick D. wrote:
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 W.
thanks to everyone who posted. This one worked for what i was doing
i = 0
while @question_array[i].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 
2008/1/30, Mark Mr [email protected]:
thanks to everyone who posted. This one worked for what i was doing
i = 0
while @question_array[i].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
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.