Returning argument as a number problem

H

Haze Noc

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
 
S

Stefano Crocco

Alle luned=C3=AC 13 agosto 2007, Haze Noc ha scritto:
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 =3D ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
random =3D ""
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 =3D~ /^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

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

random($1.to_i) if $msg =3D~ /^rand (\d+)/

I hope this helps

Stefano
 
S

Sebastian Hungerecker

Haze said:
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
 
S

Stefano Crocco

Alle luned=C3=AC 13 agosto 2007, Stefano Crocco ha scritto:
Alle luned=C3=AC 13 agosto 2007, Haze Noc ha scritto:
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 =3D ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
random =3D ""
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 =3D~ /^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

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 =3D~ /^rand (\d+)/

I hope this helps

Stefano

I forgot you also need to change the beginning of the random method. Since =
len=20
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
 
H

Haze Noc

Sebastian said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,266
Messages
2,571,318
Members
48,002
Latest member
EttaPfeffe

Latest Threads

Top