Strange behaviour with Ruby's RE operator...

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

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

puts 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!
 
S

Schüle Daniel

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

puts 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
 

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,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top