Ruby 1.8.4 behaviour - 2 questions


#1

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


#2

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?


#3

Hi Jim,

James B. Byrne wrote:

qpCCCMR1 does not match
The passed regexp is:
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 D.


#4

On Fri, 10 Feb 2006, James B. Byrne wrote:

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


#5

DÅ?a Å tvrtok 09 Február 2006 17:51 removed_email_address@domain.invalid napísal:

target_f += { source_f ? source_f : 0.00 }

target_f += source_f.to_f

I’d have used:

target_f += source_f || 0.0

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

David V.


#6

On 2/9/06, James B. Byrne removed_email_address@domain.invalid wrote:

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 = Regexp.new(f_nam_re_s) produces this:
The regexp used is:
/^QPCCCMR[[:digit:]]$/

whereas f_nam_re = 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 = “^QPCCCMR[[:digit:]]$”
f_nam_re = Regexp.new(f_nam_re_s, Regexp::MULTILINE)

=> /^QPCCCMR[[:digit:]]$/m

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

f_nam_re_s = “^QPCCCMR[[:digit:]]$”
f_nam_re = /#{f_nam_re_s}/m

=> /^QPCCCMR[[:digit:]]$/m

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

Jacob F.

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


#7

On Fri, 10 Feb 2006, David V. wrote:

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

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

target_f += Float(source_f)

since

harp:~ > ruby -r yaml -e’ target_f = 40.0; y target_f +=
“junk”.to_f ’
— 40.0

and

harp:~ > ruby -r yaml -e’ target_f = 40.0; y target_f +=
Float(“junk”) ’
-e:1:in `Float’: invalid value for Float(): “junk” (ArgumentError)
from -e:1

but

harp:~ > ruby -r yaml -e’ target_f = 40.0; y target_f += Float(“2”)

— 42.0

cheers.

-a


#8

DÅ?a Piatok 10 Február 2006 04:08 removed_email_address@domain.invalid napísal:

-a

Well, to_f coerces any junk to 0.0. It depends in that case whether you
want
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 += Float(source_f || nil)

David V.


#9

DÅ?a Piatok 10 Február 2006 20:52 Jacob F. napísal:

right? :slight_smile:

Jacob F.

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

Funny it took someone that long to notice.

David V.


#10

On 2/10/06, David V. removed_email_address@domain.invalid wrote:

But this should work as desired -and- not accept any line noise for source_f:

    target_f += Float(source_f || nil)

You of course mean

target_f += Float(source_f || 0.0)

right? :slight_smile:

Jacob F.


#11

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