Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > YAENQ: Variable as a pattern

Reply
Thread Tools

YAENQ: Variable as a pattern

 
 
basi
Guest
Posts: n/a
 
      08-16-2005
(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

 
Reply With Quote
 
 
 
 
Adam Sanderson
Guest
Posts: n/a
 
      08-16-2005
String interpolation seems to work on regular expressions so try:
"goodness" =~ /[#{v}]/

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

enjoy,
.adam

 
Reply With Quote
 
 
 
 
Joel VanderWerf
Guest
Posts: n/a
 
      08-16-2005
basi wrote:
> (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.


Same as with a string:

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

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


 
Reply With Quote
 
Timothy Hunter
Guest
Posts: n/a
 
      08-16-2005
basi wrote:
> (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
>


"goodness" =~ /#{v}/
 
Reply With Quote
 
basi
Guest
Posts: n/a
 
      08-17-2005
thanks to all who replied. so simple. (goodness, why didn't I think of
that. sigh.)

basi


Timothy Hunter wrote:
> basi wrote:
> > (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
> >

>
> "goodness" =~ /#{v}/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:26 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:25 PM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM
boolean endsWith(String s, Pattern pattern) lepikhin@gmail.com Java 17 11-16-2005 10:31 AM



Advertisments