[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Strange behaviour with Ruby's RE operator...

Just Another Victim of the Ambient Morality

4/18/2006 11:21:00 PM

Okay, the following line will print the number 0 correctly:

puts 0

> 0

Embarrassingly enough, I come from a C/C++ background (that's not the
embarrassing part), so I thought of this as a cast from the FIXNUM type, 0,
to a string type, "0" (this is the embarassing part). Of course, after the
following test failed to evaluate to true:

if 0 =~ /0/
puts "Yes!"
end

...it occurred to me that Ruby is dynamically typed. The implementation
of puts simply called the to_s method of the FIXNUM 0 and, hence, prints as
it does. Obviously, the regular expression operator =~ does no such thing,
or the test above would evaluate to true.
My question is this: why doesn't =~ call to_s? Surely, it needed the
string data of the parameter passed into it, right? How could it have not
called to_s? Why wouldn't we want it to? If "puts x" works with any x with
a to_s defined, is it unreasonable to expect the same convenience with our
regular expression operators? Or any operation on strings, for that matter?
I'm just wondering, since I'm a funny sort who like to understand
things...
Thanks for your responses!



1 Answer

Daniel Schüle

4/19/2006 3:43:00 AM

0

Just Another Victim of the Ambient Morality schrieb:
> Okay, the following line will print the number 0 correctly:
>
> puts 0
>
>> 0
>
> Embarrassingly enough, I come from a C/C++ background (that's not the
> embarrassing part), so I thought of this as a cast from the FIXNUM type, 0,
> to a string type, "0" (this is the embarassing part). Of course, after the
> following test failed to evaluate to true:
>
> if 0 =~ /0/
> puts "Yes!"
> end
>
> ...it occurred to me that Ruby is dynamically typed. The implementation
> of puts simply called the to_s method of the FIXNUM 0 and, hence, prints as
> it does. Obviously, the regular expression operator =~ does no such thing,
> or the test above would evaluate to true.
> My question is this: why doesn't =~ call to_s? Surely, it needed the
> string data of the parameter passed into it, right? How could it have not
> called to_s? Why wouldn't we want it to? If "puts x" works with any x with
> a to_s defined, is it unreasonable to expect the same convenience with our
> regular expression operators? Or any operation on strings, for that matter?
> I'm just wondering, since I'm a funny sort who like to understand
> things...
> Thanks for your responses!


because this would be a mess

irb(main):007:0> class Fixnum
irb(main):008:1> def =~ o
irb(main):009:2> self + o
irb(main):010:2> end
irb(main):011:1> end
=> nil
irb(main):012:0> 1 =~ -1
=> 0
irb(main):013:0>
irb(main):014:0* 1 =~ -11
=> -10
irb(main):015:0>

irb(main):017:0* class String
irb(main):018:1> def =~ o
irb(main):019:2> puts "bad"
irb(main):020:2> end
irb(main):021:1> end
=> nil
irb(main):022:0> "1" =~ /1/
=> 0
irb(main):023:0> "n=1" =~ /1/
=> 2
irb(main):024:0>


so how can one be sure 0 =~ /0/ does the right thing

it's a bad thing to have too much ambigity
too much to think of all possible possibilities

I absolutely prefer "" ~ // variant

puts 0
puts "0"
puts 0.0

works as cout in C++ without explicit to_string cast or whatever
because this is what everybody would expect
however more complex and not obvious operations should be explicit

Regards