[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

ArgumentError even if variable is set

Prabhas Gupte

4/3/2008 5:49:00 AM

I am writing an eruby script (rhtml). Depending on value of a particular
variable, I display number of input boxes.
If that param is not passed, I am setting variable to some value.
It works fine for first time. But when I click on the link, used to
increase input fields, I get ArgumentError even if the variable is set
to proper value.
Here is the code snippet:
-------------------------------------------------
:::

unless cgi.params['numdomains'].empty? then
$numdomains = cgi.params['numdomains']
else
$numdomains = 3
end

:::::

for dcnt in 0...$numdomains
# code to disply input items
end

:::
--------------------------------------------------
Do anyone have any idea, what may be going wrong?
--
Posted via http://www.ruby-....

6 Answers

Julian Leviston

4/3/2008 7:39:00 AM

0

This is because cgi.params["numdomains"] will return a string.

As a range can't be from a number to a string, you get the following
problem:

in irb:
>> (0.."30")
ArgumentError: bad value for range
from (irb):1

What you need to do is run "to_i" on the string.

unless cgi.params['numdomains'].empty? then
$numdomains = cgi.params['numdomains'].to_i
else
$numdomains = 3
end

That should fix your problem.

Julian.

Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
(#2) OUT NOW!
http://sensei.ze...


On 03/04/2008, at 4:48 PM, Prabhas Gupte wrote:

> I am writing an eruby script (rhtml). Depending on value of a
> particular
> variable, I display number of input boxes.
> If that param is not passed, I am setting variable to some value.
> It works fine for first time. But when I click on the link, used to
> increase input fields, I get ArgumentError even if the variable is set
> to proper value.
> Here is the code snippet:
> -------------------------------------------------
> :::
>
> unless cgi.params['numdomains'].empty? then
> $numdomains = cgi.params['numdomains']
> else
> $numdomains = 3
> end
>
> :::::
>
> for dcnt in 0...$numdomains
> # code to disply input items
> end
>
> :::
> --------------------------------------------------
> Do anyone have any idea, what may be going wrong?
> --
> Posted via http://www.ruby-....
>







Prabhas Gupte

4/3/2008 7:44:00 AM

0

Julian Leviston wrote:
> This is because cgi.params["numdomains"] will return a string.
>
> As a range can't be from a number to a string, you get the following
> problem:
>
> in irb:
> >> (0.."30")
> ArgumentError: bad value for range
> from (irb):1
>
> What you need to do is run "to_i" on the string.
>
> unless cgi.params['numdomains'].empty? then
> $numdomains = cgi.params['numdomains'].to_i
> else
> $numdomains = 3
> end
>
> That should fix your problem.
>
> Julian.
>
> Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
> (#2) OUT NOW!
> http://sensei.ze...


---------------------------------------------------------
If I do cgi.params['numdomains'].to_i then it returns another error:
undefined method `to_i' for []:Array (NoMethodError)

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

Prabhas Gupte

4/3/2008 7:49:00 AM

0


> If I do cgi.params['numdomains'].to_i then it returns another error:
> undefined method `to_i' for []:Array (NoMethodError)

Hence I did to_s first and then to_i. And it worked!
Final code segment is:
cgi.params['numdomains'].to_s.to_i

Thanks Julian!!
--
Posted via http://www.ruby-....

Julian Leviston

4/3/2008 7:51:00 AM

0

That's because your params["numdomains"] is returning a blank array,
so technically it's not empty. to_i is a method of the string class.

you could put

params["numdomains"].to_s.to_i

But I don't know why your numdomains is returning an array??

Julian


Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
(#2) OUT NOW!
http://sensei.ze...


On 03/04/2008, at 6:44 PM, Prabhas Gupte wrote:

> Julian Leviston wrote:
>> This is because cgi.params["numdomains"] will return a string.
>>
>> As a range can't be from a number to a string, you get the following
>> problem:
>>
>> in irb:
>>>> (0.."30")
>> ArgumentError: bad value for range
>> from (irb):1
>>
>> What you need to do is run "to_i" on the string.
>>
>> unless cgi.params['numdomains'].empty? then
>> $numdomains = cgi.params['numdomains'].to_i
>> else
>> $numdomains = 3
>> end
>>
>> That should fix your problem.
>>
>> Julian.
>>
>> Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
>> (#2) OUT NOW!
>> http://sensei.ze...
>
>
> ---------------------------------------------------------
> If I do cgi.params['numdomains'].to_i then it returns another error:
> undefined method `to_i' for []:Array (NoMethodError)
>
> --
> Posted via http://www.ruby-....
>





Prabhas Gupte

4/3/2008 8:02:00 AM

0

Julian Leviston wrote:
> That's because your params["numdomains"] is returning a blank array,
> so technically it's not empty. to_i is a method of the string class.
>
> you could put
>
> params["numdomains"].to_s.to_i
>
> But I don't know why your numdomains is returning an array??
>
> Julian
>
>
> Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
> (#2) OUT NOW!
> http://sensei.ze...

cgi.params is an array. And to_i method does not work on arrays.
And contents at each array index are also not strings. Hence, it is
needed to convert them to string first and then use to_i.
to_s works on any object.
--
Posted via http://www.ruby-....

Julian Leviston

4/3/2008 8:05:00 AM

0

Yes, cgi.params is an array, but cgi.params["numdomains"] shouldn't be.

Julian.


Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
(#2) OUT NOW!
http://sensei.ze...


On 03/04/2008, at 7:01 PM, Prabhas Gupte wrote:

> Julian Leviston wrote:
>> That's because your params["numdomains"] is returning a blank array,
>> so technically it's not empty. to_i is a method of the string class.
>>
>> you could put
>>
>> params["numdomains"].to_s.to_i
>>
>> But I don't know why your numdomains is returning an array??
>>
>> Julian
>>
>>
>> Learn Ruby on Rails! CHECK OUT THE FREE VIDS (LIMITED TIME) NEW VIDEO
>> (#2) OUT NOW!
>> http://sensei.ze...
>
> cgi.params is an array. And to_i method does not work on arrays.
> And contents at each array index are also not strings. Hence, it is
> needed to convert them to string first and then use to_i.
> to_s works on any object.
> --
> Posted via http://www.ruby-....
>