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)
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
> 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#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/
> 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")
"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
> "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.
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