<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Lucky-Dip Blog: Tag ruby</title>
    <link>http://blog.lucky-dip.net/articles/tag/ruby</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Random wallpaper from socwall.com</title>
      <description>&lt;p&gt;I quite like http://socwall.com. They have a rss feed for recent posts, but I wrote a small script to download a random selection instead.&lt;/p&gt;
&lt;p&gt;
On OSX I could then set my desktop background to be a random picture from a folder. In my case that folder was /Users/brad/Pictures/Wallpapers/ but that could be easily changed.
&lt;/p&gt;
&lt;p&gt;
The code is after the jump.
&lt;/p&gt;

&lt;pre class="code"&gt;
#!/usr/bin/ruby

require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'Pathname'
 
URL = 'http://socwall.com/browse/index.php?wpSortby=8'
DEST = "/Users/brad/Pictures/Wallpapers/"


def clear_old_images
  Pathname.new(DEST).children.each { |f| f.delete }
end

def download_images(count)
  count = count.to_i
  
  while count &gt; 0
    doc = Hpricot(open(URL))
    
    doc.search("div.wpThumbnail").each do |thumb|
      next if count == 0
      
      thumbnail = thumb.at("img")['src']
      image = thumbnail.gsub(/\/tb_/, '/')
      image = image.gsub(" ", "%20")
      
      remote_image = open(image, "User-Agent" =&gt; "Ruby/#{RUBY_VERSION}")
      local_image = open("#{ DEST }/#{ count }.png", 'w')
      local_image.write(remote_image.read)
      
      remote_image.close
      local_image.close
      
      count -= 1
    end
  end
end

count = ARGV[0] || 10

clear_old_images
download_images(count)

&lt;/pre&gt;</description>
      <pubDate>Mon, 01 Sep 2008 18:10:00 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:26147d2a-d19b-4164-a22e-ab03958f265c</guid>
      <author>Brad</author>
      <link>http://blog.lucky-dip.net/articles/2008/09/01/random-wallpaper-from-socwall-com</link>
      <category>ruby</category>
      <category>wallpaper</category>
      <trackback:ping>http://blog.lucky-dip.net/articles/trackback/13</trackback:ping>
    </item>
    <item>
      <title>performSelector in RubyCocoa</title>
      <description>&lt;p&gt;
I had a lot of trouble finding documentation for this. If you want to make a call on the main thread in RubyCocoa, the format is something like:
&lt;/p&gt;
&lt;pre class="code"&gt;
performSelectorOnMainThread_withObject_waitUntilDone('method_name:', object, true)
&lt;/pre&gt;

&lt;p&gt;
So two things that took me a while:
&lt;ol&gt;
&lt;li&gt;method_name has a trailing colon. &lt;/li&gt;
&lt;li&gt;The object the has the method you want to call should be the second argument. I got it in my head it should be the first for some reason.&lt;/li&gt;
&lt;/ol&gt;
&lt;/p&gt;

</description>
      <pubDate>Tue, 22 Apr 2008 15:01:00 +1000</pubDate>
      <guid isPermaLink="false">urn:uuid:dace920f-6826-4c1f-9893-6f7e58bcb965</guid>
      <author>Brad</author>
      <link>http://blog.lucky-dip.net/articles/2008/04/22/performselector-in-rubycocoa</link>
      <category>ruby</category>
      <category>cocoa</category>
      <trackback:ping>http://blog.lucky-dip.net/articles/trackback/11</trackback:ping>
    </item>
    <item>
      <title>Read table row-by-row in ruby</title>
      <description>&lt;p&gt;
I needed to export some data from a huge data table. Iterating through MyModel.find took too long and too much ram, so I went straight to the db instead. 
&lt;/p&gt;

Example code was tough to find, so here's an example:

&lt;pre class="code"&gt;
sql = "select * from #{ MyModel.table_name }"
MyModel.connection.execute(sql) do |handle|
  handle.fetch do |row|
    # row is an array of values
    ... export row ...
  end
end
&lt;/pre&gt;

</description>
      <pubDate>Wed, 12 Mar 2008 12:00:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:7f1cc0c7-4146-4e78-ab19-e968710cb720</guid>
      <author>Brad</author>
      <link>http://blog.lucky-dip.net/articles/2008/03/12/read-table-row-by-row-in-ruby</link>
      <category>ruby</category>
      <category>rails</category>
      <trackback:ping>http://blog.lucky-dip.net/articles/trackback/10</trackback:ping>
    </item>
  </channel>
</rss>
