# Returning argument as a number problem

**URL:** <https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943>\
**Category:** Ruby\
**Created:** [August 13, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943 "2007-08-13T18:47:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 13, 2007, 6:47pm UTC](https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943/1 "2007-08-13T18:47:17Z")

</div>

Hey guys, I have a method as such:

def random(len)  
if len !~ /\d+/  
pub\_send “Invalid number”  
elsif len \> 30  
pub\_send “No more then 30 chars”  
else  
chars = (“a”…“z”).to\_a + (“A”…“Z”).to\_a + (“0”…“9”).to\_a  
random = “”  
1.upto(len) { |i| newpass \<\< chars[rand(chars.length)] }  
pub\_send random  
end  
end

and i call the method like so…

random($1) if $msg =~ /^rand (\d+)/

the value of $1 will always be a number, but it seems when i am passing  
it to the function it returns an error saying it cannot compare len to  
30 because the value of len is a string… Any ideas?

Thanks in advance

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 13, 2007, 6:58pm UTC](https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943/2 "2007-08-13T18:58:38Z")

</div>

Alle lunedÃ¬ 13 agosto 2007, Haze N. ha scritto:

> ```
> 1.upto(len) { |i| newpass << chars[rand(chars.length)] }
> 
> ```
> 
> 30 because the value of len is a string… Any ideas?
> 
> Thanks in advance

No, $1 will always be a string: the $1… variables are set to the part  
of  
string matching the corresponding group. No conversion is done on it,  
you  
have to do that yourself. So, you should replace the last line with:

random($1.to\_i) if $msg =~ /^rand (\d+)/

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 13, 2007, 6:59pm UTC](https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943/3 "2007-08-13T18:59:38Z")

</div>

Haze N. wrote:

> Hey guys, I have a method as such:
> 
> def random(len)  
> if len !~ /\d+/  
> pub\_send “Invalid number”

Are you aware that if len is a number this will always be true (and it  
would  
be true no matter what regex you use).

> elsif len \> 30  
> pub\_send “No more then 30 chars”

You will only get here when len is a string (containing one or more  
digits),  
so this will raise an error because you can’t compare a string to a  
number.

> random($1) if $msg =~ /^rand (\d+)/
> 
> the value of $1 will always be a number,

No. The value of $1 is _always_ a string. “45” is not a number, it’s a  
string  
containing the character ‘4’ and ‘5’. 45 is a number.  
If you want a number call to\_i on the string.

HTH

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 13, 2007, 7:02pm UTC](https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943/4 "2007-08-13T19:02:23Z")

</div>

Oh sweet that works perfect, thanks alot

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 13, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943/5 "2007-08-13T19:04:46Z")

</div>

Sebastian H. wrote:

> Haze N. wrote:
> 
> > Hey guys, I have a method as such:
> > 
> > def random(len)  
> > if len !~ /\d+/  
> > pub\_send “Invalid number”
> 
> Are you aware that if len is a number this will always be true (and it  
> would  
> be true no matter what regex you use).

Yeah i realized that after i got it working, Wasn’t thinking straight,  
it wouldn’t work unless there was a number so there was no need for the  
regex at all, thanks again

---

<div class="post-metadata">

**Author:** ![h4z3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/h/54ee81/32.png) [@h4z3](https://www.ruby-forum.com/u/h4z3)\
**Post date:** [August 13, 2007, 7:06pm UTC](https://www.ruby-forum.com/t/returning-argument-as-a-number-problem/111943/6 "2007-08-13T19:06:38Z")

</div>

Alle lunedÃ¬ 13 agosto 2007, Stefano C. ha scritto:

> > ```
> > random = ""
> > 
> > ```
> > 
> > it to the function it returns an error saying it cannot compare len to  
> > I hope this helps
> 
> Stefano

I forgot you also need to change the beginning of the random method.  
Since len  
is now a number, you don’t need the first check:

def random(len)  
if len \>30  
pub\_send “No more then 30 chars”  
else  
…

Stefano
