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)
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 Ruby | Tags ActiveSupport | no comments | no trackbacks
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 Ruby | Tags RoR | no comments | no trackbacks
Posted by root
Sat, 09 Aug 2008 09:50:00 GMT
Time.gm(1978, 12, 3).strftime("%a")
Time.gm(1978, 12, 3).strftime("%A")
Ruby core docs
Posted in Ruby | Tags time | no comments | no trackbacks
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
result = [].empty_and_nil
Posted in Ruby | Tags array | no comments | no trackbacks
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 Ruby | Tags basis | no comments | no trackbacks
Posted by root
Mon, 04 Aug 2008 08:48:00 GMT
Posted in news | Tags impression | no comments | no trackbacks
Posted by root
Mon, 04 Aug 2008 08:41:00 GMT
alias _rmdir_empty='rmdir * 2>/dev/null'
Posted in scripts | Tags shell | no comments
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 Ruby | Tags array, hash | no comments | no trackbacks
Posted by root
Fri, 01 Aug 2008 05:58:00 GMT
Sample .bashrc file:
alias d2b='ruby -e "puts ARGV.first.to_i.to_s(2)"'
alias d2h='ruby -e "puts ARGV.first.to_i.to_s(16)"'
alias d2o='ruby -e "puts ARGV.first.to_i.to_s(8)"'
alias b2d='ruby -e "puts eval(%q|0b| + ARGV.first)"'
alias b2h='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(16)"'
alias b2o='ruby -e "puts eval(%q|0b| + ARGV.first).to_s(8)"'
alias h2d='ruby -e "puts eval(%q|0x| + ARGV.first)"'
alias h2b='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(2)"'
alias h2o='ruby -e "puts eval(%q|0x| + ARGV.first).to_s(8)"'
alias o2d='ruby -e "puts eval(%q|0o| + ARGV.first)"'
alias o2b='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(2)"'
alias o2h='ruby -e "puts eval(%q|0o| + ARGV.first).to_s(16)"'
Posted in Ruby, scripts | Tags alias | no comments | no trackbacks
Posted by root
Thu, 31 Jul 2008 11:03:00 GMT
require 'rubygems'
require 'google_translate'
tr = Google::Translate.new
tr.translate :from => "en", :to => "de", :text => "Hello, World!"
=> Hallo, Welt!
tr.translate :from => "en", :to => "es", :text => "Hello, World!"
=> Hola, Mundo!
tr = Google::Translate.new { |agent| agent.user_agent_alias = 'Mac Safari' }
The project home page
Posted in Ruby | Tags api | no comments