From: Bill Kelly Date: 2003-12-02T10:28:31+09:00 Subject: Re: anything disappearing from Ruby for 2.0? From: "Josef 'Jupp' SCHUGT" > > for i in *.tar.gz; do > mv $i ${i%.tar.gz}.tgz > done > > One way of achieving the same in Ruby is the following five-liner: > > ruby < %x(ls *\.tar\.gz).each { |x| > x.chomp! > %x(mv #{x} #{x.sub(/\.tar\.gz$/, '.tgz')}) > } > EOF Your shell solution is nice. My Ruby would have been: ruby -e "ARGV.each{|f| `mv #{f} #{f.sub(/\.tar\.gz$/,'.tgz')}`}" *.tar.gz ...or, less likely (but perhaps on Windows): ruby -rftools -e "ARGV.each{|f| File.mv(f, f.sub(/\.tar\.gz$/,'.tgz'))}" *.tar.gz > You could also use 'echo' in place of 'ls' but then you would have to > replace 'each' by 'split.each'. Of course You can also do the same in > 100% Ruby but I don't see why I should use a scripting language if > the shell allows me to do it more easily so I didn't take the time > for getting rid of ls and mv. You seem to be saying if it can be done interactively using shell commands, then good. But if it can be done easily in Ruby in a one-liner, then bad. But I like both. I'm an avid fan of Un*x shell commands... Many of my Ruby one-liners are strung together in pipes between the likes of find and grep, etc. But I've found Ruby & Perl's "one-lining" ability very freeing. What used to make my brain explode in corruscations of cut and sort and awk and sed, I can now often approach more simply [to me] in Perl or Ruby. I'd prefer Ruby. . . . For instance, I wanted to find out which entities were used in a group of HTML files, and how many times each entity occurred. (Like, – is used twice, — isn't used at all, á appears 7 times, etc...) In Ruby, I just wrote: ruby -ne "h||=Hash.new(0); scan(/&#?\w+;/){|e| h[e]+=1}; END{p h}" *.html ..Cool! Problem solved! ...Now I suppose there *might* be some way to get a comparable result with sed & sort & grep, maybe, ... I'm not sure, though. Whatever it is, I suspect it would be annoying, compared to the Ruby. :) Regards, Bill