For Loop question

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 :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

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 :slight_smile:
--
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 :slight_smile:
--
Posted via http://www.ruby-forum.com/\.

--
Thomas Preymesser
thopre@gmail.com
thomas@thopre.com
Büro: 030 - 830 353 88
mobil: 0176 - 75 03 03 04
Privat: 030 - 49 78 37 06

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 Jan 29, 2008 10:11 PM, Mark Mr <pimea.mark@gmail.com> wrote:

. 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 :slight_smile:

--
Rasputnik :: Jack of All Trades - Master of Nuns
http://number9.hellooperator.net/

Mark Mr 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 :slight_smile:

"isnt blank" != nil

···

--
Posted via http://www.ruby-forum.com/\.

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.

Crap, I mean

if !part.nil?

Jason

···

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 :slight_smile:
> --
> Posted via http://www.ruby-forum.com/\.
>
>

7stud -- wrote:

"isnt blank" != nil

Whoops. That should be:

"blank" != nil

···

--
Posted via http://www.ruby-forum.com/\.

Dick Davies 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 Williamsson

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 :slight_smile:

···

--
Posted via http://www.ruby-forum.com/.

7stud -- wrote:

7stud -- wrote:

"isnt blank" != nil

Whoops. That should be:

"blank" != nil

Double whoops. I guess "blank" does equal nil.

···

--
Posted via http://www.ruby-forum.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

···

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

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 :slight_smile:

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