<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Helder's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Mon, 06 Oct 2008 05:47:36 GMT</pubDate>
    <description>DZone Snippets: Helder's Code Snippets</description>
    <item>
      <title>Ruby Hash Power-Set</title>
      <link>http://snippets.dzone.com/posts/show/4895</link>
      <description>// adds a #power_set method to Hash class&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# The Array power set is stolen from http://snippets.dzone.com/posts/show/3524&lt;br /&gt;class Array&lt;br /&gt;  # Returns the "power set" for this Array. This means that an array with all&lt;br /&gt;  # subsets of the array's elements will be returned.&lt;br /&gt;  def power_set&lt;br /&gt;    # the power set line is stolen from http://johncarrino.net/blog/2006/08/11/powerset-in-ruby/&lt;br /&gt;    inject([[]]){|c,y|r=[];c.each{|i|r&lt;&lt;i;r&lt;&lt;i+[y]};r}&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;class Hash&lt;br /&gt;  def power_set&lt;br /&gt;    # Returns the "power set" for this Hash. This means that a array with hashes of all&lt;br /&gt;    # subsets of the hash's (key =&gt; value) pairs will be returned.&lt;br /&gt;    # Example:&lt;br /&gt;    # &gt;&gt; {:feedback_type=&gt;"", :language_code=&gt;"", :comment=&gt;""}.power_set&lt;br /&gt;    #&lt;br /&gt;    # [{}, {:comment=&gt;""}, {:language_code=&gt;""}, {:language_code=&gt;"", :comment=&gt;""}, {:feedback_type=&gt;""}, {:feedback_type=&gt;"", :comment=&gt;""}, {:feedback_type=&gt;"", :language_code=&gt;""}, {:feedback_type=&gt;"", :language_code=&gt;"", :comment=&gt;""}]&lt;br /&gt;    &lt;br /&gt;    hash_to_array = self.to_a&lt;br /&gt;    array_power_set = hash_to_array.power_set&lt;br /&gt;    hash_power_set = array_power_set.collect { |pairs| pairs.inject({}) { |hash,pair| hash[pair[0]] = pair[1]; hash } }&lt;br /&gt;    hash_power_set&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 13 Dec 2007 12:14:16 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4895</guid>
      <author>helder (Helder Ribeiro)</author>
    </item>
    <item>
      <title>Empty a Gmail label with FireWatir</title>
      <link>http://snippets.dzone.com/posts/show/3961</link>
      <description>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.&lt;br /&gt;&lt;br /&gt;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.&lt;br /&gt;&lt;br /&gt;By the way, it assumes Gmail is in German. Replace that string with the one that comes up for your language.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def empty_label(label)&lt;br /&gt;  # you need a Firefox instance already running with JSSh installed and listening&lt;br /&gt;  ff = Firefox.new&lt;br /&gt;  # Goes to Gmail in HTML&lt;br /&gt;  ff.goto('http://mail.google.com/mail/h/x4odcwnm5f5v/?s=l&amp;l=#{label}')&lt;br /&gt;  a = []&lt;br /&gt;  # doing 'c.set' didn't work for me here so I had to do this hack of getting the value&lt;br /&gt;  # and then acquiring the element thorugh ff.checkbox(:value,value)&lt;br /&gt;  ff.checkboxes.each {|c| a &lt;&lt; c.value}&lt;br /&gt;&lt;br /&gt;  while a.length &gt; 0 do&lt;br /&gt;    c = []&lt;br /&gt;    a.each {|v| c &lt;&lt; ff.checkbox(:value,v)}&lt;br /&gt;    # checks all checkboxes&lt;br /&gt;    c.each {|e| e.set}&lt;br /&gt;    # clicks the drop-down entry for deleting&lt;br /&gt;    ff.select_list(:name,'tact').select('In den Papierkorb verschieben')&lt;br /&gt;    ff.button(:name,'nvp_tbu_go').click&lt;br /&gt;    a = []&lt;br /&gt;    ff.checkboxes.each {|c| a &lt;&lt; c.value}&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 07 May 2007 21:54:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3961</guid>
      <author>helder (Helder Ribeiro)</author>
    </item>
  </channel>
</rss>
