[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby 1.8.4 behaviour - 2 questions

James B. Byrne

2/9/2006 4:08:00 PM

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


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

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


9 Answers

James B. Byrne

2/9/2006 4:13:00 PM

0



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?
>
>


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


Matthew Desmarais

2/9/2006 4:39:00 PM

0

Hi Jim,

James B. Byrne wrote:
> OS = CentOS 4.2
> Ruby = 1.8.4
> (http://dev.centos.org/centos/4.2/testing/i386/RPMS/ruby-1.8.4-1.c...)
>
>
> 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



Ara.T.Howard

2/9/2006 4:52:00 PM

0

Jacob Fugal

2/9/2006 5:01:00 PM

0

On 2/9/06, James B. Byrne <ByrneJB@hartelyne.ca> 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 Fugal

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


Ara.T.Howard

2/10/2006 3:08:00 AM

0

David Vallner

2/10/2006 1:21:00 PM

0

Dna Piatok 10 Február 2006 04:08 ara.t.howard@noaa.gov napísal:
> 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

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 Vallner


Jacob Fugal

2/10/2006 7:52:00 PM

0

On 2/10/06, David Vallner <david@vallner.net> 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? :)

Jacob Fugal


David Vallner

2/12/2006 3:46:00 AM

0

Dna Piatok 10 Február 2006 20:52 Jacob Fugal napísal:
> On 2/10/06, David Vallner <david@vallner.net> 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? :)
>
> Jacob Fugal

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

Funny it took someone that long to notice.

David Vallner


James Byrne

2/15/2006 4:46:00 PM

0

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


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