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.
2e0bd0e6da47b9db55ac548a0d71fdb3?d=identicon&s=25 Tim Kuntz (Guest)
on 2006-05-26 13: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
D695ecbcb6de95b36f1dfe6685650ee2?d=identicon&s=25 mitchell (Guest)
on 2006-05-26 13:16
Tim Kuntz 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/
37ee5fa90f5eaeef62553629382497f7?d=identicon&s=25 Leslie Viljoen (Guest)
on 2006-05-26 13:44
(Received via mailing list)
On 5/26/06, mitchell <fs8206@yahoo.com> 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?
2e0bd0e6da47b9db55ac548a0d71fdb3?d=identicon&s=25 Tim Kuntz (Guest)
on 2006-05-26 14: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?
B33ea5c12d767bfd1253940a960274f5?d=identicon&s=25 Tim Hunter (timhunter)
on 2006-05-26 14:05
Leslie Viljoen wrote:
> On 5/26/06, mitchell <fs8206@yahoo.com> 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.
956f185be9eac1760a2a54e287c4c844?d=identicon&s=25 ts (Guest)
on 2006-05-26 14:14
(Received via mailing list)
>>>>> "T" == Tim Kuntz <timkuntz@gmail.com> 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
2e0bd0e6da47b9db55ac548a0d71fdb3?d=identicon&s=25 Tim Kuntz (Guest)
on 2006-05-27 06: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.
Bc6d88907ce09158581fbb9b469a35a3?d=identicon&s=25 James Britt (Guest)
on 2006-05-27 07:31
(Received via mailing list)
Tim Hunter 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 Britt

"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.