Forum: Ruby command substitution not working

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
1642e34277869bcee021b398d292254f?d=identicon&s=25 Anil Kumar (anil_77)
on 2016-11-22 20:29
Trying find and replace with the following code:

#!/usr/bin/env ruby

file_names = ['foo.txt']

file_names.each do |file_name|
  text =
  new_contents = text.gsub(/ADMIN_URL=\"t3:.*/,
'ADMIN_URL=\"t3s://`hostname -f`.*')

  # To merely print the contents of the file, use:
     puts new_contents
  #     # To write changes to the file, use:, "w") {|file| file.puts new_contents }

But command substitution is not happening. Not sure what is the issue
here. Any help?
C2d1339038b5fec16b5b15878f648d96?d=identicon&s=25 Jesus Castello (jesus_castello)
on 2016-11-23 17:15
You need to use double quotes & string interpolation.

When you use single quotes you are saying that it should take the
characters inside in a literal way & not perform any kind of operations
with them.


text.gsub(/ADMIN_URL=\"t3:.*/, "ADMIN_URL=\"t3:#{`something`}")

I hope that helps.
- Jesus Castello,
This topic is locked and can not be replied to.