Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Apply an arithmetic operation using 2 hashes

   1   users_hash = {"tom" => 1,  
   2   "adi" => 1,
   3   "adi2" => 1,    
   4   "aaron" => 1 }
   5  
   6   users_hash2 = {"tom" => 2,  
   7   "adi" => 3,  
   8   "aaron" => 4 }
   9  
  10  class Hash
  11    def +(hash2)
  12      self.each do |key, value|
  13        self[key] += hash2[key] if hash2.has_key? key
  14      end
  15    end
  16  end
  17  
  18  c = users_hash + users_hash2


=> {"adi2"=>1, "adi"=>4, "tom"=>3, "aaron"=>5}

update: 14:35 5-Sep-08
In addition, the following code copies over items from the right hash which are not present in the left hash:
   1  class Hash
   2    def +(hash2)
   3      temp = Hash.new
   4      self.each do |key, value|
   5        temp[key] = self[key] + hash2[key] unless hash2[key].nil?
   6      end
   7  
   8      hash2.update(temp)
   9      self.update(hash2)
  10    end
  11  end
  12  
  13  c = users_hash + users_hash2

==> {"adi2"=>1, "adi3"=>3, "adi"=>1, "tom"=>3, "aaron"=>5}

Reference:
- Ruby hash merge (Add element to Ruby hash) - by Ruby on Rails [rubyonrailsexamples.com]
- Manipulating Structured Data in Ruby > Working with Hashes [informit.com]

Increment a date using Ruby

This Ruby code converts a string into a date and increments the day, week, month, quarter or year.

   1  
   2  def date_add(sdate='', unit='',i=0)
   3  
   4    sdate[/(\d+)\/(\d+)\/(\d+)\s(\d+):(\d+):(\d+)/]
   5    iyear = $3.to_i; imonth = $2.to_i; iday = $1.to_i; ihour = $4.to_i; imin = $5.to_i; isec = $6.to_i
   6    
   7    case  unit
   8      when 'days'
   9        t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
  10        t1 += (60 * 60 * 24 * i)
  11      when 'weeks'
  12        t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
  13        t1 += (60 * 60 * 24 * 7 * i) 
  14      when 'months'
  15        imonth += i
  16        if imonth < 12 then
  17          t1 = Time.local(iyear,imonth+i,iday,ihour,imin,isec)
  18        else
  19          t1 = Time.local(iyear+=1,imonth -12,iday,ihour,imin,isec)
  20        end
  21      when 'quarter'
  22        imonth += 3
  23        if imonth <= 12 then
  24          t1 = Time.local(iyear,imonth,iday,ihour,imin,isec)
  25        else
  26          t1 = Time.local(iyear+=1,imonth - 12,iday,ihour,imin,isec)
  27        end    
  28      when 'years'
  29        t1 = Time.local(iyear+i,imonth,iday,ihour,imin,isec)
  30      else
  31        raise 'not a valid date unit'
  32    end
  33    t1
  34  end

   1  
   2  date_add("17/03/2008 17:48:00",'months',2)

output: Sat May 17 17:48:00 +0100 2008

Addition for hashes in Ruby

Why, oh, why is there no addition for hashes in Ruby? 'update' can do the trick, but only returns what was updated, rather than the whole updated hash. It also forces an update rather than being passive.

Sometimes you only want to temporarily do an add, and this is how I pulled it off:

   1  class Hash
   2    def +(add)
   3      temp = {}
   4      add.each{|k,v| temp[k] = v}
   5      self.each{|k,v| temp[k] = v}
   6      temp
   7    end
   8  end


Now you can do stuff like:

   1  x = { :a => 1 }
   2  y = { :b => 2 }
   3  
   4  # x + y => { :a => 1, :b => 2 }, but x and y are untouched
   5  # x + { :c => 3 } => { :a => 1, :c => 3 }


If you want to force an add, it's easy:

   1  x += y => { :a => 1, :b => 2 } (this is now what x contains)
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS