0

We know this:

p '%s' % "test"
#=> "test"

But is it possible to do this:

s = "string"
s[/#{%s}/] % "test"
0

1 Answer 1

2

Well, you could always just do this:

s[Regexp.new("%s" % "test")]

If your string can contain characters with special meaning, use Regexp.escape beforehand. Also, you can directly interpolate into a regular expression:

s = "test"
/#{s}/ #=> /test/
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.