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

YAENQ: Variable as a pattern

5 views
Skip to first unread message

basi

unread,
Aug 16, 2005, 6:09:41 PM8/16/05
to
(I ordered for a copy of Mastering Regular Expressions. Should be in my
snail mailbox anytime soon. In the meantime ... )

How do I tell Ruby to use the content of a variable as the pattern,
rather than interpreting the variable name as a literal?


v = "aeiou"

Then, in

"goodness" =~ /[v]/

I'd like v to be interepreted not as a literal but as a variable name.

Thanks
basi

Adam Sanderson

unread,
Aug 16, 2005, 6:16:32 PM8/16/05
to
String interpolation seems to work on regular expressions so try:
"goodness" =~ /[#{v}]/

Or more verbosely:
"goodness" =~ Regexp.new("[#{v}]")

enjoy,
.adam

Joel VanderWerf

unread,
Aug 16, 2005, 6:22:04 PM8/16/05
to

Same as with a string:

"goodness" =~ /[#{v}]/

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


Timothy Hunter

unread,
Aug 16, 2005, 6:26:11 PM8/16/05
to

"goodness" =~ /#{v}/

basi

unread,
Aug 17, 2005, 12:41:45 PM8/17/05
to
thanks to all who replied. so simple. (goodness, why didn't I think of
that. sigh.)

basi

0 new messages