Forum: Ruby String Match method =~ error

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Tim K. (Guest)
on 2006-05-26 15:13
(Received via mailing list)
The Ruby doc states the following for a String#=~

 "cat o' 9 tails" =~ '\d'   #=> nil

When I run what looks like the same command, I get the following error.

irb(main):001:0> "cat o' 9 tails" =~ '\d'
TypeError: type mismatch: String given
        from (irb):1:in `=~'
        from (irb):1

I have tried this on a couple of different environments but always
Ruby 1.8.4. Can anyone provide me with any insight as to why I don't
get the nil return value?

tim
mitchell (Guest)
on 2006-05-26 15:16
Tim K. wrote:
> The Ruby doc states the following for a String#=~
>
>  "cat o' 9 tails" =~ '\d'   #=> nil
>
> When I run what looks like the same command, I get the following error.
>
> irb(main):001:0> "cat o' 9 tails" =~ '\d'
> TypeError: type mismatch: String given
>         from (irb):1:in `=~'
>         from (irb):1
>
> I have tried this on a couple of different environments but always
> Ruby 1.8.4. Can anyone provide me with any insight as to why I don't
> get the nil return value?
>
> tim

"cat o' 9 tails" =~ /\d/
Leslie V. (Guest)
on 2006-05-26 15:44
(Received via mailing list)
On 5/26/06, mitchell <removed_email_address@domain.invalid> wrote:
> >         from (irb):1
> >
> > I have tried this on a couple of different environments but always
> > Ruby 1.8.4. Can anyone provide me with any insight as to why I don't
> > get the nil return value?
> >
> > tim
>
> "cat o' 9 tails" =~ /\d/

ie. Ruby Doc is wrong?
Tim K. (Guest)
on 2006-05-26 16:05
(Received via mailing list)
I found the answer. The Ruby doc is wrong for the Ruby 1.8.4.

http://www.ruby-doc.org/core/classes/String.html#M001849

According to the "Programming Ruby" book, at least one operand must be
a Regexp. Older versions of Ruby would convert the second operand to a
Regexp if both were Strings.

Does anyone happen to know what version this changed with?
Tim H. (Guest)
on 2006-05-26 16:05
Leslie V. wrote:
> On 5/26/06, mitchell <removed_email_address@domain.invalid> wrote:
>> >         from (irb):1
>> >
>> > I have tried this on a couple of different environments but always
>> > Ruby 1.8.4. Can anyone provide me with any insight as to why I don't
>> > get the nil return value?
>> >
>> > tim
>>
>> "cat o' 9 tails" =~ /\d/
>
> ie. Ruby Doc is wrong?

More likely just outdated. What "Ruby Doc" are you reading? I believe
this behavior changed between 1.6 and 1.8.
ts (Guest)
on 2006-05-26 16:14
(Received via mailing list)
>>>>> "T" == Tim K. <removed_email_address@domain.invalid> writes:

T> Does anyone happen to know what version this changed with?

 1.8.2

svg% ./ruby -ve '"aa" =~ "a"'
ruby 1.8.2 (2004-12-25) [i686-linux]
-e:1:in `=~': type mismatch: String given (TypeError)
	from -e:1
svg%

 1.8.1 give

svg% ./ruby -ve '"aa" =~ "a"'
ruby 1.8.1 (2003-12-25) [i686-linux]
-e:1: warning: string =~ string will be obsolete; use explicit regexp
svg%


Guy Decoux
Tim K. (Guest)
on 2006-05-27 08:27
(Received via mailing list)
Guy >  1.8.2

Thanks.

Tim >> ie. Ruby Doc is wrong?

Leslie > More likely just outdated. What "Ruby Doc" are you reading?

I thought the same but I did verify that the 1.8.4 docs are incorrect.
The link I posted was to the 1.8.4 core doc @ ruby-doc.org.
James B. (Guest)
on 2006-05-27 09:31
(Received via mailing list)
Tim H. wrote:
>>>>tim
>>>
>>>"cat o' 9 tails" =~ /\d/
>>
>>ie. Ruby Doc is wrong?
>
>
> More likely just outdated.

Maybe, but it *should* be getting regenerate each night after fetching
the source from cvs



--
James B.

"I was born not knowing and have had only a little
  time to change that here and there."
  - Richard P. Feynman
This topic is locked and can not be replied to.