Active Support Date and Time

Posted by root Sun, 10 Aug 2008 13:38:00 GMT

n = Time.now

(n.at_end_of_week - n.at_beginning_of_week) / (24 * 3600)

# 6.99998842592593

Shouldn’t it be 7 exactly?

Probably because the start of a week is 00:00:00 and the end is 23:59:59:
>> Time.now.at_beginning_of_week
=> Mon Aug 04 00:00:00 +0300 2008

>> Time.now.at_end_of_week
=> Sun Aug 10 23:59:59 +0300 2008

Posted in  | Tags  | no comments | no trackbacks

Setting Core Objects' Methods in RoR

Posted by root Sat, 09 Aug 2008 12:14:00 GMT

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.

Posted in  | Tags  | no comments | no trackbacks

Getting the Weekday

Posted by root Sat, 09 Aug 2008 09:50:00 GMT

# if the birthdate is 1978-12-03

Time.gm(1978, 12, 3).strftime("%a") 
Time.gm(1978, 12, 3).strftime("%A") 

# the weekday day is "Sun" (%a), "Sunday" (%A)

Ruby core docs

Posted in  | Tags  | no comments | no trackbacks

Return nil Instead Of empty Array instance

Posted by root Fri, 08 Aug 2008 10:35:00 GMT

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

Posted in  | Tags  | no comments | no trackbacks

How false Is nil

Posted by root Mon, 04 Aug 2008 13:59:00 GMT

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

Posted in  | Tags  | no comments | no trackbacks

The Most Dangerous Beast

Posted by root Mon, 04 Aug 2008 08:48:00 GMT

Posted in  | Tags  | no comments | no trackbacks

Remove All Empty Directories

Posted by root Mon, 04 Aug 2008 08:41:00 GMT

alias _rmdir_empty='rmdir * 2>/dev/null'

Posted in  | Tags  | no comments

Multiplication Table In AoH Structure

Posted by root Sun, 03 Aug 2008 18:09:00 GMT

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

Posted in  | Tags ,  | no comments | no trackbacks

Converting Between Numeric Bases

Posted by root Fri, 01 Aug 2008 05:58:00 GMT

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)"'

Posted in ,  | Tags  | no comments | no trackbacks

Google::Translate, A Simple API In Ruby

Posted by root Thu, 31 Jul 2008 11:03:00 GMT

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

Posted in  | Tags  | no comments

Older posts: 1 2 3 4 ... 9