Random strings

Aug 19
2008
module Kernel
  def random_uc(size=16)
    Array.new(size){ rand(26)+65 }.pack('C*')
  end
  def random_lc(size=16)
    Array.new(size){ rand(26)+97 }.pack('C*')
  end
  def random_num(size=16)
    Array.new(size){ rand(10) }.join
  end
end
 
random_uc => "HRTWYJBJEZDKYPTJ"
random_lc => "onxkvkezqukspmcx"
random_num => "5862195932579950"

Setting Core Objects’ Methods in RoR

Aug 09
2008

mkdir -p RAILS_ROOT/vendor/plugins/core_methods

mkdir -p RAILS_ROOT/vendor/plugins/core_methods/init.rb

The content of init.rb:

module Rails
 
  empty_and_nil = %q{def empty_and_nil; self.empty?? nil : self end}
 
  Array.send :class_eval, empty_and_nil
  Hash.send  :class_eval, empty_and_nil
 
end

The namespace Rails isn’t necessary, it’s a cosmetic feature.

Return nil Instead Of empty Array instance

Aug 08
2008
class Array
  def empty_and_nil
    self.empty?? nil : self
  end
end
 
result = [1,2,3].empty_and_nil # [1, 2, 3]
result = [].empty_and_nil        # nil

How false Is nil

Aug 04
2008

Use nil when the return is a boolean value.
Use false when the return is either false or not true object.
Otherwise they have pretty much the same semantic:

nil == false => false
nil.nil?        => true
false.nil?     => false
true.nil?      => false
 
if !nil && !false
  puts 'true'
end
 
if nil.nil? && !true.nil?
  puts 'true'
end
 
>> TrueClass.ancestors
=> [TrueClass, Object, Kernel]
>> FalseClass.ancestors
=> [FalseClass, Object, Kernel]
>> NilClass.ancestors
=> [NilClass, Object, Kernel]

Ruby core docs

Multiplication Table In AoH Structure

Aug 03
2008
mt = Array.new(10) { |e| Hash.new { |h,k| h[k] = k * e } }
 
mt[1][2] => 2
mt[2][2] => 4
mt[5][4] => 20
mt[9][5] => 45

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 }

Calendar

September 2010
M T W T F S S
« Aug    
 12345
6789101112
13141516171819
20212223242526
27282930  

Tags