# Problem in array

**URL:** <https://www.ruby-forum.com/t/problem-in-array/134713>\
**Category:** Ruby\
**Created:** [April 14, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/problem-in-array/134713 "2008-04-14T12:55:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pranjal\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pranjal\_jain](https://www.ruby-forum.com/u/pranjal_jain)\
**Post date:** [April 14, 2008, 12:55pm UTC](https://www.ruby-forum.com/t/problem-in-array/134713/1 "2008-04-14T12:55:19Z")

</div>

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:in`test\_b\_add\_activity’

2 tests, 0 assertions, 0 failures, 1 errors

Can anyone suggest me the solution for this?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![pranjal\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pranjal\_jain](https://www.ruby-forum.com/u/pranjal_jain)\
**Post date:** [April 14, 2008, 1:10pm UTC](https://www.ruby-forum.com/t/problem-in-array/134713/2 "2008-04-14T13:10:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pranjal\_jain](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ac91a4/32.png) [@pranjal\_jain](https://www.ruby-forum.com/u/pranjal_jain)\
**Post date:** [April 14, 2008, 1:34pm UTC](https://www.ruby-forum.com/t/problem-in-array/134713/3 "2008-04-14T13:34:09Z")

</div>

2008/4/14, Pranjal J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> 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
