<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Jswizard's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 01:58:24 GMT</pubDate>
    <description>DZone Snippets: Jswizard's Code Snippets</description>
    <item>
      <title>Create a UNIX init.d startup script with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2710</link>
      <description>&lt;br /&gt;&lt;code&gt;&lt;br /&gt;#!/usr/bin/env ruby&lt;br /&gt;#&lt;br /&gt;# app_name      This is a startup script for use in /etc/init.d&lt;br /&gt;#&lt;br /&gt;# chkconfig:    2345 80 20&lt;br /&gt;# description:  Description of program / service&lt;br /&gt;&lt;br /&gt;APP_NAME = 'app_name'&lt;br /&gt;&lt;br /&gt;case ARGV.first&lt;br /&gt;	when 'status':&lt;br /&gt;    	status = 'stopped'&lt;br /&gt;        puts "#{APP_NAME} is #{status}"&lt;br /&gt;	when 'start':&lt;br /&gt;		# Do your thang&lt;br /&gt;    when 'stop':&lt;br /&gt;		# Do your thang&lt;br /&gt;	when 'restart':&lt;br /&gt;		# Do your thang		&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;unless %w{start stop restart status}.include? ARGV.first&lt;br /&gt;        puts "Usage: #{APP_NAME} {start|stop|restart}"&lt;br /&gt;        exit&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 25 Sep 2006 21:30:43 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2710</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>UTF-8 compatible String ranges in Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2553</link>
      <description>As found at &lt;a href="http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123935"&gt;http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/123935&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class String&lt;br /&gt;        def [] (*params)&lt;br /&gt;                if params.all? { |p| Integer===p } ||&lt;br /&gt;                   params.size==1 &amp;&amp; Range===params[0]&lt;br /&gt;                        res = self.unpack("U*").[](*params)&lt;br /&gt;                        res = [res] unless Array===res&lt;br /&gt;                        return res.pack("U*")&lt;br /&gt;                end&lt;br /&gt;                super&lt;br /&gt;        end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 06 Sep 2006 06:25:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2553</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Create YAML test fixtures from database in Rails</title>
      <link>http://snippets.dzone.com/posts/show/2525</link>
      <description>As found at &lt;a href="http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake"&gt;http://media.pragprog.com/titles/fr_rr/code/CreateFixturesFromLiveData/lib/tasks/extract_fixtures.rake&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;&lt;br /&gt;task :extract_fixtures =&gt; :environment do&lt;br /&gt;  sql  = "SELECT * FROM %s"&lt;br /&gt;  skip_tables = ["schema_info"]&lt;br /&gt;  ActiveRecord::Base.establish_connection&lt;br /&gt;  (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;    i = "000"&lt;br /&gt;    File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;      data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;      file.write data.inject({}) { |hash, record|&lt;br /&gt;        hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;        hash&lt;br /&gt;      }.to_yaml&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 04 Sep 2006 01:48:31 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2525</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Turn Rake task list into hash with Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2475</link>
      <description>&lt;code&gt;&lt;br /&gt;Hash[*(`rake -T`.split(/\n/).collect { |l| l.match(/rake (\S+)\s+\#\s(.+)/).to_a }.collect { |l| [l[1], l[2]] }).flatten] &lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 27 Aug 2006 06:35:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2475</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Exempt directories from Apache's ProxyPass directive</title>
      <link>http://snippets.dzone.com/posts/show/2433</link>
      <description>Very useful if forwarding to Mongrel, etc.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;        ProxyPass /stylesheets !&lt;br /&gt;        ProxyPass /javascripts !&lt;br /&gt;        ProxyPass /images !     &lt;br /&gt;        ProxyPass / http://127.0.0.1:3010/&lt;br /&gt;        ProxyPassReverse / http://127.0.0.1:3010/  &lt;/code&gt;</description>
      <pubDate>Thu, 17 Aug 2006 05:25:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2433</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Using Rio from Ruby to easily save a Web page to file</title>
      <link>http://snippets.dzone.com/posts/show/2432</link>
      <description>Found at &lt;a href="http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/"&gt;http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# (sudo) gem install rio&lt;br /&gt;require 'rubygems'&lt;br /&gt;require 'rio'&lt;br /&gt;# open an URI an copy the content into a file&lt;br /&gt;rio('http://www.juretta.com/') &gt; rio('juretta_index.html')&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Cool syntax!</description>
      <pubDate>Wed, 16 Aug 2006 21:38:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2432</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Use the del.icio.us API via HTTPS from Ruby</title>
      <link>http://snippets.dzone.com/posts/show/2431</link>
      <description>Found at &lt;a href="http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/"&gt;http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'net/https'&lt;br /&gt;require "rexml/document"&lt;br /&gt;&lt;br /&gt;username = "" # your del.icio.us username&lt;br /&gt;password = "" # your del.icio.us password&lt;br /&gt;&lt;br /&gt;resp = href = "";&lt;br /&gt;begin      &lt;br /&gt;  http = Net::HTTP.new("api.del.icio.us", 443)&lt;br /&gt;  http.use_ssl = true&lt;br /&gt;  http.start do |http|&lt;br /&gt;    req = Net::HTTP::Get.new("/v1/tags/get", {"User-Agent" =&gt; &lt;br /&gt;        "juretta.com RubyLicious 0.2"})&lt;br /&gt;    req.basic_auth(username, password)&lt;br /&gt;    response = http.request(req)&lt;br /&gt;    resp = response.body&lt;br /&gt;  end     &lt;br /&gt;  #  XML Document&lt;br /&gt;  doc = REXML::Document.new(resp)    &lt;br /&gt;  # iterate over each element &lt;tag count="200" tag="Rails"/&gt;&lt;br /&gt;  doc.root.elements.each do |elem|&lt;br /&gt;    print elem.attributes['tag']  + " -&gt; " + elem.attributes['count'] + "\n"&lt;br /&gt;  end&lt;br /&gt;  &lt;br /&gt;rescue SocketError&lt;br /&gt;  raise "Host " + host + " nicht erreichbar"&lt;br /&gt;rescue REXML::ParseException =&gt; e&lt;br /&gt;  print "error parsing XML " + e.to_s&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Aug 2006 21:37:15 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2431</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Load a Web page in Ruby and print information</title>
      <link>http://snippets.dzone.com/posts/show/2430</link>
      <description>Found at &lt;a href="http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/"&gt;http://www.juretta.com/log/2006/08/13/ruby_net_http_and_open-uri/&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require 'open-uri'&lt;br /&gt;require 'pp'&lt;br /&gt;&lt;br /&gt;open('http://www.juretta.com/') do |f|&lt;br /&gt;  # hash with meta information&lt;br /&gt;  pp  f.meta&lt;br /&gt;   &lt;br /&gt;  # &lt;br /&gt;  pp "Content-Type: " + f.content_type&lt;br /&gt;  pp "last modified" + f.last_modified.to_s&lt;br /&gt;  &lt;br /&gt;  no = 1&lt;br /&gt;  # print the first three lines&lt;br /&gt;  f.each do |line|&lt;br /&gt;    print "#{no}: #{line}"&lt;br /&gt;    no += 1&lt;br /&gt;    break if no &gt; 4&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 16 Aug 2006 21:36:10 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2430</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Put borders around Web page elements with CSS</title>
      <link>http://snippets.dzone.com/posts/show/2377</link>
      <description>Found at &lt;a href="http://homepage.mac.com/chrispage/iblog/C42511381/E20060806095030/index.html"&gt;http://homepage.mac.com/chrispage/iblog/C42511381/E20060806095030/index.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  * { outline: 2px dotted red }&lt;br /&gt;  * * { outline: 2px dotted green }&lt;br /&gt;  * * * { outline: 2px dotted orange }&lt;br /&gt;  * * * * { outline: 2px dotted blue }&lt;br /&gt;  * * * * * { outline: 1px solid red }&lt;br /&gt;  * * * * * * { outline: 1px solid green }&lt;br /&gt;  * * * * * * * { outline: 1px solid orange }&lt;br /&gt;  * * * * * * * * { outline: 1px solid blue }&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 Aug 2006 05:05:12 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2377</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
    <item>
      <title>Ordinals for All Numeric Types</title>
      <link>http://snippets.dzone.com/posts/show/2368</link>
      <description>This snippet will extend all numerical types with a method for returning the English ordinal, i.e. 1st, 2nd, 3rd, 4th, etc.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class Numeric&lt;br /&gt;  def ordinal&lt;br /&gt;    cardinal = self.to_i.abs&lt;br /&gt;    if (10...20).include?(cardinal) then&lt;br /&gt;      cardinal.to_s &lt;&lt; 'th'&lt;br /&gt;    else&lt;br /&gt;      cardinal.to_s &lt;&lt; %w{th st nd rd th th th th th th}[cardinal % 10]&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt; &lt;br /&gt;[1, 22, 123, 10, -3.1415].collect { |i| i.ordinal }&lt;br /&gt;=&gt; ["1st", "22nd", "123rd", "10th", "3rd"]&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.</description>
      <pubDate>Fri, 04 Aug 2006 22:46:04 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/2368</guid>
      <author>jswizard (JavaScript Wizard)</author>
    </item>
  </channel>
</rss>
