Interpolated regular expression

Regular expressions is the swiss army knife in the Ruby tool box. For example they are used to replace a matching word like: ~~~ ruby “Java is an awesome language.”.gsub(/Java/, ‘Ruby’) # => “Ruby is an awesome language.” ~~~

If the matching pattern has work dynamically, the Ruby percentage notation offers an interpolated regular expression. The example from above again as interpolated regular expression:

language = 'Java'
assert = "#{language} is an awesome language."
assert.gsub(/#{language}/, 'Ruby')
# => "Ruby is an awesome language."

Interpolating works as simple as injecting the variable into the regular expression surrounded by hashing curly brackets.
Another regexp interpolation, checking for the Ruby minor version, looks like:

version = "2.2"
ruby = "ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-linux]"
ruby.scan /^ruby\s*(#{version})/ # => [["2.2"]]

This one interpolates a string into the regexp on the fly.