I was following a Java VS Perl discussion on a web board that I read.
Naturally I wanted to see what the Ruby version would look like, so I
grabbed the Perl code and and pasted into a file. I commented out all
the Perl code and inserted the ruby version. The file looked like this
…
#!/usr/bin/perl -w
use strict;
my %frequency = ();
$frequency{$_}++ for (split /\W/, <>);
print “$: $frequency{$}\n” for (keys %frequency);
frequency = Hash.new(0)
ARGF.read.split(/\W/).each { |w| next if w == ‘’; frequency[w] += 1}
frequency.each { |k,v| puts “#{k}: #{v}” }
I then ran the program like this …
ruby wordfreq.rb inputfile
And got the response …
Unquoted string “frequency” may clash with future reserved word at
wordfreq.rb line 7.
Semicolon seems to be missing at wordfreq.rb line 7.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
Unquoted string “frequency” may clash with future reserved word at
wordfreq.rb line 8.
Unquoted string “w” may clash with future reserved word at wordfreq.rb
line 8.
syntax error at wordfreq.rb line 8, near ")
ARGF"
Execution of wordfreq.rb aborted due to compilation errors.
What!? These didn’t look like Ruby error messages! Is Ruby now
embedding a perl interpreter?
Actually, I understand what happened. Ruby looked at the #! comment and
politely loaded the Perl interpreter. It just surprized me for a
moment.
···
–
– Jim Weirich jweirich@one.net http://w3.one.net/~jweirich
“Beware of bugs in the above code; I have only proved it correct,
not tried it.” – Donald Knuth (in a memo to Peter van Emde Boas)