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

About this user

Helder Ribeiro http://obvio171.wordpress.com

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

Ruby Hash Power-Set

// adds a #power_set method to Hash class

   1  
   2  # The Array power set is stolen from http://snippets.dzone.com/posts/show/3524
   3  class Array
   4    # Returns the "power set" for this Array. This means that an array with all
   5    # subsets of the array's elements will be returned.
   6    def power_set
   7      # the power set line is stolen from http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/
   8      inject([[]]){|c,y|r=[];c.each{|i|r<<i;r<<i+[y]};r}
   9    end
  10  end
  11  
  12  class Hash
  13    def power_set
  14      # Returns the "power set" for this Hash. This means that a array with hashes of all
  15      # subsets of the hash's (key => value) pairs will be returned.
  16      # Example:
  17      # >> {:feedback_type=>"", :language_code=>"", :comment=>""}.power_set
  18      #
  19      # [{}, {:comment=>""}, {:language_code=>""}, {:language_code=>"", :comment=>""}, {:feedback_type=>""}, {:feedback_type=>"", :comment=>""}, {:feedback_type=>"", :language_code=>""}, {:feedback_type=>"", :language_code=>"", :comment=>""}]
  20      
  21      hash_to_array = self.to_a
  22      array_power_set = hash_to_array.power_set
  23      hash_power_set = array_power_set.collect { |pairs| pairs.inject({}) { |hash,pair| hash[pair[0]] = pair[1]; hash } }
  24      hash_power_set
  25    end
  26  end
  27  

Empty a Gmail label with FireWatir

When you have a huge label with thousands of messages, Gmail can't handle deleting all of them at a time (it says it can, but for me it's never worked). This solves the problem.

I wrote it more as a practice to get familiar with FireWatir, so it's not very pretty or anything and it's a bit slow. If you have doubts on how to use or suggestions on how to improve it please feel free to contact me.

By the way, it assumes Gmail is in German. Replace that string with the one that comes up for your language.

   1  
   2  def empty_label(label)
   3    # you need a Firefox instance already running with JSSh installed and listening
   4    ff = Firefox.new
   5    # Goes to Gmail in HTML
   6    ff.goto('http://mail.google.com/mail/h/x4odcwnm5f5v/?s=l&l=#{label}')
   7    a = []
   8    # doing 'c.set' didn't work for me here so I had to do this hack of getting the value
   9    # and then acquiring the element thorugh ff.checkbox(:value,value)
  10    ff.checkboxes.each {|c| a << c.value}
  11  
  12    while a.length > 0 do
  13      c = []
  14      a.each {|v| c << ff.checkbox(:value,v)}
  15      # checks all checkboxes
  16      c.each {|e| e.set}
  17      # clicks the drop-down entry for deleting
  18      ff.select_list(:name,'tact').select('In den Papierkorb verschieben')
  19      ff.button(:name,'nvp_tbu_go').click
  20      a = []
  21      ff.checkboxes.each {|c| a << c.value}
  22    end
  23  end
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS