This was just things I found in my collection of obscure ruby.
So, in Ruby, a simple no-bells
implementation of the unix command cat
would be:
#!/usr/bin/env ruby
puts ARGF.read
ARGF is your friend when it comes to
input; it is a virtual file that
gets all input from named files or all
from STDIN.
ARGF.each_with_index do |line, idx|
print ARGF.filename, ":", idx, ";", line
end
# print all the lines in every file passed via command line that contains login
ARGF.each do |line|
puts line if line =~ /login/
end
Thank goodness we didn’t get the
diamond operator in Ruby, but we did
get ARGF as a replacement. Though
obscure, it actually turns out to be
useful. Consider this program, which
prepends copyright headers in-place
(thanks to another perlism, -i) to
every file mentioned on the
command-line
#!/usr/bin/env ruby -i
Header = DATA.read
ARGF.each_line do |e|
puts Header if ARGF.pos - e.length == 0
puts e
end
__END__
#--
# Copyright (C) 2007 Fancypants, Inc.
#++
Credits to:
http://www.oreillynet.com/ruby/blog/2007/04/trivial%5Fscripting%5Fwith%5Fruby.html#comment-565558
and
http://blog.nicksieger.com/articles/2007/10/06/obscure-and-ugly-perlisms-in-ruby