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

String Substitution

Jul 30
2008
require 'benchmark'
 
n = 1_000_000
s = " : : : "
 
Benchmark.bm do |x|
  x.report("tr:")   { n.times{ s.tr(":", ";") } }
  x.report("tr_s:") { n.times{ s.tr_s(":", ";") } }
  x.report("gsub:") { n.times{ s.gsub(":", ";") } }
end

Right, tr beats the other methods:
user system total real
tr: 2.760000 0.120000 2.880000 ( 2.897022)
tr_s: 5.310000 0.240000 5.550000 ( 5.822026)
gsub: 8.850000 0.200000 9.050000 ( 9.311452)

Google Translation API for Ruby

Jul 27
2008

In essence (from bulgarian to japanese):

agent = WWW::Mechanize.new
  agent.post("http://translate.google.com/translate_t",
    "sl" => "bg",
    "tl" => "ja",
    "text" => "Здравей, свят!").search("#result_box").inner_html

results in こんにちは、世界!

three ways to print the alphabet

Jul 27
2008

String methods:

"a".upto("z"){ |i| puts i }
a = String.new "a"
0.upto(0){ p a }
1.upto(25){ p a.next! }

Range method:

("a".."z").each{ |e| puts e }

Dracula (The Vampire King)

Jul 26
2008

def and undef a method

Jul 25
2008
def my_method; end # define a method
undef my_method    # remove a method

The core manual

Ugly join implementation

Jul 25
2008
arr = (1..10).to_a
 
 str = arr * ', '
 str = arr.join(', ')

Am I going to use the perlish coding style? Not really.

Simple File Manipulation

Jul 21
2008

Ruby file manipulation is as easy as:

MyFile.create "test.txt"
MyFile.add "test.txt", "some content"
MyFile.remove "test.txt"

For example create a file caled fileeasy.rb and add the following module to it:

module FileEasy
 
  def self.included(subject)
    subject.extend(ClassMethods)
  end
 
  module ClassMethods
   alias_method :delete, :remove
   alias_method :unlink, :remove
 
    def create(filename)
      File.open(filename, "w")
    end
    def add(filename, text)
      File.open(filename, "a"){ |f| f < < text }
    end
    def remove(filename)
      File.delete(filename)
    end
  end
 
end

Then test it with irb:

require 'fileeasy'
 
class MyClass
  include FileEasy
end
 
MyFile.create "text.txt"
MyFile.add "text.txt", "test content"
MyFile.remove "text.txt"

Of course this post is just a simple tutorial and the standard library is the best choice – FileUtils.

Simple class’s variable accessor

Jul 19
2008
class Class
  def attr(arg)
    arg = arg.to_s
    if self.class_variables.include?(arg)
      class_eval %(def self.#{arg.sub(/^\@\@/, '')}; #{arg}; end)
    else
      raise ArgumentError, "No such class attribute", caller
    end
  end
end
 
class Test
  @@class_var = "class variable value"
 
  attr :@@class_var
end
 
puts Test.class_var => "class variable value"

Calendar

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

Tags