[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Problem in array

Pranjal Jain

4/14/2008 10:55:00 AM

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

2 Answers

Stefano Crocco

4/14/2008 11:09:00 AM

0

On Monday 14 April 2008, Pranjal Jain 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


Robert Klemme

4/14/2008 11:33:00 AM

0

2008/4/14, Pranjal Jain <pranjal.jain123@gmail.com>:
> 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

--
use.inject do |as, often| as.you_can - without end