Forum: Ruby command substitution not working

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 = File.read(file_name)
  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:
         File.open(file_name, "w") {|file| file.puts new_contents }
         end

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.

Example:

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

I hope that helps.
- Jesus Castello, blackbytes.info
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.