Problem in array

HI all
I am facing a small problem in array.

I am placing the values of the select list in an array. Now I want to
check if any of the values in the array is greater then the “0.0” .

I had used the following command:

if ((p[x] >= 0.0))
puts “true”
else
puts “false”
end.

I am getting the following error:

Loaded suite login
Started

Web page opened successfully
.They can be used
E
Finished in 16.074 seconds.

  1. Error:
    test_b_add_activity(TestCase):
    NoMethodError: undefined method >=' for nil:NilClass login.rb:111:intest_b_add_activity’

2 tests, 0 assertions, 0 failures, 1 errors

Can anyone suggest me the solution for this?

Thanks in advance.

On Monday 14 April 2008, Pranjal J. wrote:

Can anyone suggest me the solution for this?

Use a valid index and make sure the element of the array you’re testing
is not
nil.

Ruby is complaining that you called the >= method on nil, which doesn’t
have
such a method. Since you’re using the >= operator on the value returned
by
p[x], this may mean two things:

  1. x >= p.size: An array of size n contains n elements, with index from
    0 to
    n-1. If you try accessing an element with an index greater than that,
    Array#[]
    will return nil.
  2. x < p.size and p[x] == nil: even if the index is less than the size
    of the
    array, the array can contain nil elements. For example [1,2,nil,3] has
    nil as
    the third entry, so calling [1,2,nil,3][2] will return nil.

Since you didn’t give any information on how p is obtained, I can’t give
you a
more specific advice. If this doesn’t help you and you want more
suggestions,
please give us more information.

Stefano

2008/4/14, Pranjal J. [email protected]:

HI all
I am facing a small problem in array.

I am placing the values of the select list in an array. Now I want to
check if any of the values in the array is greater then the “0.0” .

Assuming > 0 means error:

raise “Invalid positive” if array.any? {|x| x > 0}

Alternatively

raise “Invalid positive” unless array.all? {|x| x <= 0}

Kind regards

robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs