Bulgarian-English command line tool (using sa.dir.bg)

Posted by root Sat, 19 Jul 2008 21:59:00 GMT

#!/usr/bin/env ruby

require 'tempfile'
require 'net/http'

arg =  ARGV[0].chomp
url = 'sa.dir.bg'
h   = Net::HTTP.new(url, 80)

resp,data = h.get("/cgi/sabig.cgi?word=#{arg}", nil )
outtmp    = Tempfile.new("translator")

outtmp << data.to_s
outtmp.rewind
puts %x[html2text "#{outtmp.path}"]

__END__

Posted in  | Tags  | no comments

Getting Ruby include path

Posted by root Sun, 13 Jan 2008 14:25:00 GMT

$LOAD_PATH === $: #true, hence
irb
puts$:.sort.join($/)

__END__
.
/usr/lib/ruby/1.8
/usr/lib/ruby/1.8/i386-linux
/usr/lib/ruby/1.8/i486-linux
/usr/local/lib/site_ruby
/usr/local/lib/site_ruby/1.8
/usr/local/lib/site_ruby/1.8/i386-linux
/usr/local/lib/site_ruby/1.8/i486-linux

Posted in  | Tags  | no comments

File.rename(tedious part of the filename)

Posted by root Sun, 13 Jan 2008 07:58:00 GMT

Rename each file in each sub-directory, substituting ‘%20’ with ’ ‘(space).
class File
  def self.rename_basename(abs_name, old_str, new_str)
    f_name = File.basename(abs_name).gsub Regexp.quote(old_str), new_str
    d_name = File.dirname(abs_name)
    File.rename(abs_name, d_name + '/' + f_name)
    rescue
      raise ArgumentError, "No such file #{filename}", caller
  end
end

Dir["*/**"].each do |f|
  if File.basename(f).match('%20')
    File.rename_basename(f, '%20', ' ')
  end
end

Posted in ,  | Tags  | no comments