Take heart, Don.
In spite of what some say, many here understand your attempt pretty well.
What is not easy to know, is what exactly went wrong for you, since you have not provided exact copy-and-paste of your command line. What transpires from you original post is that at some point you may have managed to have Bash pass the quoting characters as part of the argument. That is, you may have typed something like "'GNOME;'" as the first argument. (Notice how I have used both double quote " and single quote ' characters. In this case, Bash strips the double quote characters and passes the single quotes as part of the argument.
If you look at the response from Ammar Ali, you will see why this transpires from your post. We assume that your comments show output that you have received in earlier runs of the script, and the line that Ammar Ali points out shows the quoting characters included in the value of the regexp.
You ask if this is still a bug. The point is that you use quoting or escaping characters on the command line to tell Bash what you want it to consider as part of the argument. Bash then should remove such quoting characters and pass only the quoted characters. So, how come that the single quotes appeared in the output you rendered in your comments? It is not just in line 16 below, it happens also in lines 9, 12, and 29. If you command line was like your line 6, there should not have been any single quotes in the output!
When I tested it, I was surprised, see below. Perhaps you too have seen something strange.
Testing your script I have been surprised by how Ruby behaves under Cygwin, under Windows. If you are under Linux, this should probably not be of concern to you. This may be related to how the Windows native shell works, or used to work under DOS. The old DOS command.exe did not split the command line into separate words, it just passed the whole command line to the program being run, and left it to that program to do any splitting and quoting. How the modern cmd.exe behaves, I don't know.
I should perhaps note that I am not using a Cygwin port of Ruby, I am running a Windows native Ruby from a Cygwin Bash. Ruby22 and Ruby193.
Anyway, here is my testing:
$ cat -n test.rb
1 #!/usr/bin/env ruby
2
3 # This file demonstrates what I believe is a a bug
4 # in Regexp as of 2.2.5r319 (from sources),
5 # seen on Linux Mint 17.3 MATE
6 # Usage: .test_regexp 'GNOME' 'GNOME;MATE;'
7
8 puts 'String to Regexp source: ' +ARGV[0].inspect
9 # String to Regexp source: 'GNOME;'
10
11 puts 'String to replace source: ' +ARGV[1]
12 # String to replace source: 'GNOME;MATE;'
13
14 r = Regexp.new( ARGV[0])
15 puts 'Regexp as created: ' + r.to_s
16 # Regexp as created: (?-mix:'GNOME;')
17
18 line = 'OnlyShowIn=GNOME;\n'
19 puts line
20 # OnlyShowIn=GNOME;\n
21
22 newlineWRegexp = line.gsub( r, ARGV[1])
23 newlineRaw = line.gsub( /GNOME;/, ARGV[1])
24
25 puts 'Regexp: ' + newlineWRegexp
26 # Regexp: OnlyShowIn=GNOME;\n
27
28 puts 'Raw: ' + newlineRaw
29 # Raw: OnlyShowIn='GNOME;MATE;'\n
$ ruby test.rb 'GNOME;' 'GNOME;MATE;'
String to Regexp source: "GNOME;"
String to replace source: GNOME;MATE;
Regexp as created: (?-mix:GNOME;)
OnlyShowIn=GNOME;\n
Regexp: OnlyShowIn=GNOME;MATE;\n
Raw: OnlyShowIn=GNOME;MATE;\n
$ ruby test.rb "'GNOME;'" 'GNOME;MATE;'
String to Regexp source: "GNOME;" #<---- What???
String to replace source: GNOME;MATE;
Regexp as created: (?-mix:GNOME;)
OnlyShowIn=GNOME;\n
Regexp: OnlyShowIn=GNOME;MATE;\n
Raw: OnlyShowIn=GNOME;MATE;\n
$ ruby test.rb "\'GNOME;\'" 'GNOME;MATE;'
String to Regexp source: "'GNOME;'"
String to replace source: GNOME;MATE;
Regexp as created: (?-mix:'GNOME;')
OnlyShowIn=GNOME;\n
Regexp: OnlyShowIn=GNOME;\n
Raw: OnlyShowIn=GNOME;MATE;\n
What surprises me here, is that Ruby seems to do a second parsing of the quoting in the command line arguments!
Surely, Bash works as I expect, passing the single quotes as part of the argument:
$ /bin/echo -n "'GNOME;'" 'GNOME;MATE;' | od -c
0000000 ' G N O M E ; ' G N O M E ; M
0000020 A T E ;
0000024
and for good measure:
$ perl -e 'print "@ARGV"' "'GNOME;'" 'GNOME;MATE;' | od -c
0000000 ' G N O M E ; ' G N O M E ; M
0000020 A T E ;
0000024
(I am piping through "od -c" in order to show all special characters, if there are any.)
Consider what happens when I omit one of the single quotes in the first argument:
ruby test.rb "'GNOME;" 'GNOME;MATE;'
test.rb:11:in `+': can't convert nil into String (TypeError)
from test.rb:11:in `<main>'
String to Regexp source: "GNOME; GNOME;MATE;"
Line 11 is the one that outputs ARGV[1], the second argument. Apparently the Windows port of ruby interprets quoting characters received from Bash, and when the single quotes are unbalanced, it joined the arguments into one. As a consequence, ARGV[1] was nil.
If instead I use the native Windows command line shell, "cmd.exe", things again work as expected.
···
Den 06.06.2016 07:18, skrev Don Wilde:
Hassn,
You have completely confused me. I'm honest enough to admit that I do
not know WTF you are talking about. How can this not be as simple as
my example?
On 6/5/16, Hassan Schroeder <hassan.schroeder@gmail.com> wrote:
On Sun, Jun 5, 2016 at 8:03 PM, Don Wilde <dwilde1@gmail.com> wrote:
But wouldn't that still be a bug? BASH won't accept GNOME; on
thecommand line, would it? I had problems when I tried to include
unquoted /GNOME;/
21:14 ~/testcases/standalone(master)[ruby-2.3.1@standalone]$ cat arg.rb
#!/usr/bin/env ruby
puts "#{ARGV[0] || 'nah'} #{ARGV[1] || 'nope'}"
21:14 ~/testcases/standalone(master)[ruby-2.3.1@standalone]$ ./arg.rb WAT;
NOW
WAT nope
bash: NOW: command not found
21:14 ~/testcases/standalone(master)[ruby-2.3.1@standalone]$ ./arg.rb WAT\;
NOW
WAT; NOW
21:14 ~/testcases/standalone(master)[ruby-2.3.1@standalone]$
HTH,
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
Hassan Schroeder | about.me
twitter: @hassan
Consulting Availability : Silicon Valley or remote