Converting Between Numeric Bases

Jul 31
2008

Sample .bashrc file:

# decimal to binary
alias d2b='ruby -e "puts ARGV.first.to_i.to_s(2)"'
# decimal to hexademical
alias d2h='ruby -e "puts ARGV.first.to_i.to_s(16)"'
# decimal to octal
alias d2o='ruby -e "puts ARGV.first.to_i.to_s(8)"'
 
# binary to decimal
alias b2d='ruby -e "puts eval(%q|0b| + ARGV.first)"'
# binary to hexademical
alias b2h='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(16)"'
# binary to octal
alias b2o='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(8)"'
 
# hexademical to decimal
alias h2d='ruby -e "puts eval(%q|0x| + ARGV.first)"'
# hexademical to binary
alias h2b='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(2)"'
# hexademical to octal
alias h2o='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(8)"'
 
# octal to decimal
alias o2d='ruby -e "puts eval(%q|0o| + ARGV.first)"'
# octal to binary
alias o2b='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(2)"'
# octal to hexademical
alias o2h='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(16)"'

Google::Translate, A Simple API In Ruby

Jul 31
2008
require 'rubygems'
require 'google_translate'
 
tr = Google::Translate.new
 
# from English to German
tr.translate :from => "en", :to => "de", :text => "Hello, World!"
 => Hallo, Welt!
 
# from English to Spanish
tr.translate :from => "en", :to => "es", :text => "Hello, World!"
 => Hola, Mundo!
 
# WWW::Mechanize constructor usage
tr = Google::Translate.new { |agent| agent.user_agent_alias = 'Mac Safari' }

The project home page

Calendar

July 2008
M T W T F S S
« Jun   Aug »
 123456
78910111213
14151617181920
21222324252627
28293031  

Tags