Ruby - Interpolate text quickly in regex

We know this:

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

But is it possible to do this:

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

Answers


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/

Need Your Help

Passing variable when redirecting to a page

php laravel laravel-4

I have designed a contact page with the inputs validation, if the validation has successfully passed then email be will be sent. I am having problem after email has been sent, the $sent variable is...

laravel model and table naming with underscore

php laravel

I am trying to create model named CustomDataStore (models/custom_data_store.php) and it is extending Eloquent, so table is named as custom_data_stores, but it gives me error.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.