R
Rob Sanheim
I figure that I must be missing something really obvious with this
question. But here goes:
I want to know if a string matches a regular expression. I don't care
where the match begins, I don't want a matchdata object, I don't want
to check for nil, I just want a boolean indicating if a string matches
a regexp. And I want this to be one method call. From what I can
tell, this is the closest I can get:
reg = /\w/
reg === "string" # close, but I want to send the message to the
string, not the other way around
("string" =~ reg) >= 0 # ugly
("string" =~ reg) != 0 # still ugly
"string".include? reg # this would seem to follow the ruby way, but it
throws a TypeError
If I'm not missing some other trick to do this, why doesn't include?
just allow regexp's like so many of the other (slice, index, scan,
etc) string methods?
- Rob
question. But here goes:
I want to know if a string matches a regular expression. I don't care
where the match begins, I don't want a matchdata object, I don't want
to check for nil, I just want a boolean indicating if a string matches
a regexp. And I want this to be one method call. From what I can
tell, this is the closest I can get:
reg = /\w/
reg === "string" # close, but I want to send the message to the
string, not the other way around
("string" =~ reg) >= 0 # ugly
("string" =~ reg) != 0 # still ugly
"string".include? reg # this would seem to follow the ruby way, but it
throws a TypeError
If I'm not missing some other trick to do this, why doesn't include?
just allow regexp's like so many of the other (slice, index, scan,
etc) string methods?
- Rob