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

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