Ruby 1.8.4 behaviour - 2 questions

J

James B. Byrne

OS = CentOS 4.2
Ruby = 1.8.4
(http://dev.centos.org/centos/4.2/testing/i386/RPMS/ruby-1.8.4-1.c4.i386.rpm)


Problem 1.

I am using Regexp.new() and my unit test alters behaviour for the same
regexp string depending upon whether or not any option is added to the
call.

Thus f_nam_re = Regexp.new(f_nam_re_s) produces this:

The passed regexp is:
^QPCCCMR[[:digit:]]$
The regexp used is:
/^QPCCCMR[[:digit:]]$/
$= is: false
qpcccmr1 does not match
QPCCCMR1 matches
QPCCMMR1 does not match
qpCCCMR1 does not match
QPCCCMRC does not match
QPCCCMR1.csv does not match
QPCCCMR9 matches
XXQPCCCMR9 does not match
QPCCCMR9yyy does not match
XyZQPCCCMR3AbC does not match


whereas f_nam_re = Regexp.new(f_nam_re_s, ["m") produces this:

The passed regexp is:
^QPCCCMR[[:digit:]]$
The regexp used is:
/^QPCCCMR[[:digit:]]$/i
$= is: false
qpcccmr1 matches
QPCCCMR1 matches
QPCCMMR1 does not match
qpCCCMR1 matches
QPCCCMRC does not match
QPCCCMR1.csv does not match
QPCCCMR9 matches
XXQPCCCMR9 does not match
QPCCCMR9yyy does not match
XyZQPCCCMR3AbC does not match

I get the same result if I substitute "x" for "m" in the call. What is
going on?


Problem 2.

I wish to assign a float value conditional on the source being non-nil.
I thought that this construction would work;

target_f += { source_f ? source_f : 0.00 }

but it does not. Is there a one line idiom that will assign from a
potentially nil float value obtained from a database?

I suppose that I could do this:

if !source_f then
target_f += 0.0
else
target_f += !source_f
end

But for aesthetic reasons I prefer to have this construct in a single
line.

Thanks,
Jim
 
J

James B. Byrne

Please ignore the "[" typo in the original post. The actual construct
used is given below:
Problem 1.

whereas f_nam_re = Regexp.new(f_nam_re_s, "m") produces this:

The passed regexp is:
^QPCCCMR[[:digit:]]$
The regexp used is:
/^QPCCCMR[[:digit:]]$/i
$= is: false
qpcccmr1 matches
QPCCCMR1 matches
QPCCMMR1 does not match
qpCCCMR1 matches
QPCCCMRC does not match
QPCCCMR1.csv does not match
QPCCCMR9 matches
XXQPCCCMR9 does not match
QPCCCMR9yyy does not match
XyZQPCCCMR3AbC does not match

I get the same result if I substitute "x" for "m" in the call. What is
going on?
 
M

Matthew Desmarais

Hi Jim,
OS = CentOS 4.2
Ruby = 1.8.4
(http://dev.centos.org/centos/4.2/testing/i386/RPMS/ruby-1.8.4-1.c4.i386.rpm)


Problem 1.

I am using Regexp.new() and my unit test alters behaviour for the same
regexp string depending upon whether or not any option is added to the
call.

Thus f_nam_re = Regexp.new(f_nam_re_s) produces this:

The passed regexp is:
^QPCCCMR[[:digit:]]$
The regexp used is:
/^QPCCCMR[[:digit:]]$/
$= is: false
qpcccmr1 does not match
QPCCCMR1 matches
QPCCMMR1 does not match
qpCCCMR1 does not match
QPCCCMRC does not match
QPCCCMR1.csv does not match
QPCCCMR9 matches
XXQPCCCMR9 does not match
QPCCCMR9yyy does not match
XyZQPCCCMR3AbC does not match


whereas f_nam_re = Regexp.new(f_nam_re_s, ["m") produces this:

The passed regexp is:
^QPCCCMR[[:digit:]]$
The regexp used is:
/^QPCCCMR[[:digit:]]$/i
$= is: false
qpcccmr1 matches
QPCCCMR1 matches
QPCCMMR1 does not match
qpCCCMR1 matches
QPCCCMRC does not match
QPCCCMR1.csv does not match
QPCCCMR9 matches
XXQPCCCMR9 does not match
QPCCCMR9yyy does not match
XyZQPCCCMR3AbC does not match

I get the same result if I substitute "x" for "m" in the call. What is
going on?
Everything here is working correctly. If you look at the documentation
for Regexp.new and pay attention to the part about specifying options
you should see the correct way to accomplish what (I'm guessing) you're
trying to do.
Problem 2.

I wish to assign a float value conditional on the source being non-nil.
I thought that this construction would work;

target_f += { source_f ? source_f : 0.00 }
The "{" character is the wrong thing to use here. You'll want to try
this (assuming that target_f already has a value assigned to it):
target_f += ( source_f ? source_f : 0.00 )
Hope that helps.

Regards,
Matthew Desmarais
 
A

ara.t.howard

Problem 1.

I am using Regexp.new() and my unit test alters behaviour for the same
regexp string depending upon whether or not any option is added to the
call.

all according to the docs.

target_f += { source_f ? source_f : 0.00 }

target_f += source_f.to_f

regards.

-a
 
J

Jacob Fugal

I am using Regexp.new() and my unit test alters behaviour for the same
regexp string depending upon whether or not any option is added to the
call.

The key is the difference here:
Thus f_nam_re =3D Regexp.new(f_nam_re_s) produces this:
The regexp used is:
/^QPCCCMR[[:digit:]]$/
whereas f_nam_re =3D Regexp.new(f_nam_re_s, ["m") produces this:
The regexp used is:
/^QPCCCMR[[:digit:]]$/i

The second regex is coming out case insensitive (and not multiline).
To see why this is, check the Regex documentation[1]. The second
parameter to Regexp.new needs to be a Fixnum (ie. integer). Otherwise,
any other non-nil value (so "m" counts) simply switches the regex to
case-insensitive. What you want to use instead of "m" is
Regexp::MULTILINE[2], eg.:

f_nam_re_s =3D "^QPCCCMR[[:digit:]]$"
f_nam_re =3D Regexp.new(f_nam_re_s, Regexp::MULTILINE)
# =3D> /^QPCCCMR[[:digit:]]$/m

Another alternative is to simply build it using the regexp literal syntax:

f_nam_re_s =3D "^QPCCCMR[[:digit:]]$"
f_nam_re =3D /#{f_nam_re_s}/m
# =3D> /^QPCCCMR[[:digit:]]$/m

As you can see, string interpolation works fine in the literal syntax.

Jacob Fugal

[1] http://www.ruby-doc.org/core/classes/Regexp.html#M001526
[2] This used to be Regexp::pOSIXLINE in 1.6
 
D

David Vallner

D=C5=88a =C5=A0tvrtok 09 Febru=C3=A1r 2006 17:51 (e-mail address removed) nap=
=C3=ADsal:
target_f +=3D source_f.to_f

I'd have used:

target_f +=3D source_f || 0.0

myself, but after hacking at irb a bit, ara's method seems somewhat sexier=
=20
than that rather standard Ruby idiom. A nice perk is that it won't bug out =
on=20
when the database returns the data as a String.

David Vallner
 
A

ara.t.howard

D=C5=88a =C5=A0tvrtok 09 Febru=C3=A1r 2006 17:51 (e-mail address removed) na= p=C3=ADsal:

I'd have used:

=09target_f +=3D source_f || 0.0

myself, but after hacking at irb a bit, ara's method seems somewhat sexie= r
than that rather standard Ruby idiom. A nice perk is that it won't bug ou= t on
when the database returns the data as a String.

hmmm. if that's the case (possible string) i'd use

target_f +=3D Float(source_f)

since

harp:~ > ruby -r yaml -e' target_f =3D 40.0; y target_f +=3D "junk".to=
_f '
--- 40.0

and

harp:~ > ruby -r yaml -e' target_f =3D 40.0; y target_f +=3D Float("ju=
nk") '
-e:1:in `Float': invalid value for Float(): "junk" (ArgumentError)
from -e:1

but

harp:~ > ruby -r yaml -e' target_f =3D 40.0; y target_f +=3D Float("2"=
) '
--- 42.0

cheers.

-a

--=20
happiness is not something ready-made. it comes from your own actions.
- h.h. the 14th dali lama
 
D

David Vallner

D=C5=88a Piatok 10 Febru=C3=A1r 2006 04:08 (e-mail address removed) nap=C3=ADs=
al:
hmmm. if that's the case (possible string) i'd use

target_f +=3D Float(source_f)

since

harp:~ > ruby -r yaml -e' target_f =3D 40.0; y target_f +=3D "junk".= to_f=20
' --- 40.0

and

harp:~ > ruby -r yaml -e' target_f =3D 40.0; y target_f +=3D Float("= junk")
' -e:1:in `Float': invalid value for Float(): "junk" (ArgumentError) from
-e:1

but

harp:~ > ruby -r yaml -e' target_f =3D 40.0; y target_f +=3D Float("= 2") '
--- 42.0

cheers.

-a

Well, to_f coerces any junk to 0.0. It depends in that case whether you wan=
t=20
magic or exact behavior. However, Float(nil) results in a TypeError.

But this should work as desired -and- not accept any line noise for source_=
f:

target_f +=3D Float(source_f || nil)

David Vallner
 
J

Jacob Fugal

But this should work as desired -and- not accept any line noise for sourc= e_f:

target_f +=3D Float(source_f || nil)

You of course mean

target_f +=3D Float(source_f || 0.0)

right? :)

Jacob Fugal
 
D

David Vallner

D=C5=88a Piatok 10 Febru=C3=A1r 2006 20:52 Jacob Fugal nap=C3=ADsal:
You of course mean

target_f +=3D Float(source_f || 0.0)

right? :)

Jacob Fugal

I need to lay off kitten huffing, it seems. Of course I meant that.

=46unny it took someone that long to notice.

David Vallner
 
J

James B. Byrne

Thanks for all the help and the insightful discussion of what is going
on behind the purple (ruby??) curtain. I tried to answer last week but
I kept getting an Internal Server Error when connecting to
ruby-forum.com, thus the delay.

Regards,
Jim
 

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
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top