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
···
On Jan 29, 2008 5:11 PM, Mark Mr <pimea.mark@gmail.com> wrote:
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
--
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.
···
On 29/01/2008, Mark Mr <pimea.mark@gmail.com> wrote:
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
--
Posted via http://www.ruby-forum.com/\.
On Jan 29, 2008 5:15 PM, Jason Roelofs <jameskilton@gmail.com> wrote:
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
On Jan 29, 2008 5:11 PM, Mark Mr <pimea.mark@gmail.com> wrote:
> 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
> --
> Posted via http://www.ruby-forum.com/\.
>
>
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
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
···
2008/1/30, Mark Mr <pimea.mark@gmail.com>:
--
use.inject do |as, often| as.you_can - without end