Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string =~ string expressions

249 views
Skip to first unread message

Mark James

unread,
Aug 24, 2005, 11:08:02 AM8/24/05
to
Hi,

According to both http://www.ruby-doc.org/core/classes/String.html#M001334
and http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String._eq_sd
I should be able to type:
"cat o' 9 tails" =~ "\\d"
and get a match result.

But in Ruby 1.8.2 I'm getting the error:

`=~': type mismatch: String given (TypeError)

Robert Klemme

unread,
Aug 24, 2005, 11:33:13 AM8/24/05
to

I believe this has changed at some place in time (between 1.6 and 1.8 as
far as I can recall). It seems the documentation is not up to date.

I'd prefer to use /\d/ =~ "cat o' 9 tails", "cat o' 9 tails" =~ /\d/ or
"cat o' 9 tails" =~ Regexp.new "\\d" - assuming "\\d" is some kind of user
entered string.

Kind regards

robert

gregarican

unread,
Aug 24, 2005, 11:35:06 AM8/24/05
to
Make James wrote:

> and http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#S...


> I should be able to type:
> "cat o' 9 tails" =~ "\\d"
> and get a match result.


> But in Ruby 1.8.2 I'm getting the error:


> `=~': type mismatch: String given (TypeError)

Try "cat o' 9 tails" =~ /\d/

Mark James

unread,
Aug 24, 2005, 11:55:40 AM8/24/05
to
Robert Klemme wrote:

> I'd prefer to use /\d/ =~ "cat o' 9 tails", "cat o' 9 tails" =~ /\d/ or
> "cat o' 9 tails" =~ Regexp.new "\\d" - assuming "\\d" is some kind of user
> entered string.

Thanks Robert, I am trying to match against a r.e. held in a string variable.
It works as: "cat o' 9 tails" =~ (Regexp.new "\\d")

Chris Game

unread,
Aug 24, 2005, 5:34:12 PM8/24/05
to
gregarican wrote:

"In previous versions of Ruby, both [operands] could be strings, in
which case the second operand was converted into a regular
expression behind the scenes" - p.69 of the cited book above.

--
Chris Game

"Everything that can be said can be said clearly."
-- Ludwig Wittgenstein

Mark James

unread,
Aug 24, 2005, 9:47:59 PM8/24/05
to
Chris Game wrote:

> "In previous versions of Ruby, both [operands] could be strings, in
> which case the second operand was converted into a regular
> expression behind the scenes" - p.69 of the cited book above.

OK, thanks. This must be in the Second Edition of the book
<http://www.ruby-doc.org/docs/ProgrammingRuby/>, not
the First Edition on the Web.

Chris Game

unread,
Aug 25, 2005, 5:27:14 AM8/25/05
to
Mark James wrote:

Right. The 2nd Ed. still needs better editing though - hopefully a
3rd Ed. will come out eventually.

--
Chris Game

Calm down -- it's only ones and zeroes

0 new messages