<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0">
<channel>
	<title>Ruby and Rails Snippets</title><description>Ruby and Rails Snippets</description><link>http://app.feed.informer.com/digest3/rubyonbr_snippets.html</link>
											<copyright>Respective post owners and feed distributors</copyright>
											<generator>http://feed.informer.com/</generator>

<item>
	<title>POST an XML request through the REST API</title>
	<description>This script demonstrates how to post an XML request through the REST API, and retrieve a short URL from rubyurl.com.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;require 'net/http'
&lt;br /&gt;
&lt;br /&gt;url = "rubyurl.com" # for testing
&lt;br /&gt;path = "/api/links"
&lt;br /&gt;headers = {"Content-Type" =&gt; "text/xml"}
&lt;br /&gt;
&lt;br /&gt;h = Net::HTTP.new(url, 80)
&lt;br /&gt;h.use_ssl = false
&lt;br /&gt;
&lt;br /&gt;xmlrequest = "&lt;link&gt;
&lt;br /&gt;  &lt;website_url&gt;http://github.com/robbyrussell&lt;/website_url&gt;
&lt;br /&gt;&lt;/link&gt;"
&lt;br /&gt;resp, data = h.post(path, xmlrequest, headers)
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;output:
&lt;br /&gt;&lt;pre&gt;    
&lt;br /&gt;=&gt; [#&lt;Net::HTTPOK 200 OK readbody=true&gt;, "&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;&lt;link&gt;&lt;website_url&gt;http://github.com/robbyrussell&lt;/website_url&gt;&lt;permalink&gt;http://rubyurl.com/PNsF&lt;/permalink&gt;&lt;/link&gt;"]
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;rcscript usage:
&lt;br /&gt;&lt;code&gt;rcscript //job:rubyurl http://rorbuilder.info/r/short_url.rsf http://www.wired.com/
&lt;br /&gt;#=&gt; http://rubyurl.com/b20P
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Resources:
&lt;br /&gt; - &lt;a href="http://groups.google.com/group/rubyonrails-talk/msg/b1d193e4565df087"&gt;UPS Shipping Tools XML Integration - Ruby on Rails: Talk | Google Groups&lt;/a&gt; [google.com]
&lt;br /&gt; - &lt;a href="http://groups.google.com/group/rubyonrails-talk/msg/ae9f020826dcb265"&gt;Best way to parse XML data - Ruby on Rails: Talk | Google Groups&lt;/a&gt; [google.com]
&lt;br /&gt; - &lt;a href="http://www.ruby-forum.com/topic/133283"&gt;constructing an (xml) request properly for http post to an api - Ruby Forum&lt;/a&gt; [ruby-forum.com]
&lt;br /&gt; - &lt;a href="http://www.ruby-doc.org/stdlib/libdoc/net/http/rdoc/index.html"&gt;net/http: Ruby Standard Library Documentation&lt;/a&gt; [ruby-doc.org]
&lt;br /&gt; - &lt;a href="http://rubyurl.com/api"&gt;RubyURL » Keep it short (and sweet)&lt;/a&gt; [rubyurl.com]
&lt;br /&gt; - &lt;a href="http://rorbuilder.info/r/short_url.xml#rubyurl"&gt;short_url.xml&lt;/a&gt; [rorbuilder.info]&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/wjiA8cJQvDc" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/wjiA8cJQvDc/7537</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/wjiA8cJQvDc/7537?</guid>
	<pubDate>Thu, 02 Jul 2009 08:58 GMT</pubDate>

</item>

<item>
	<title>Use a stronger password with a crib sheet</title>
	<description>This script accepts a simple user password, and translates each character to a 2 or 3 digit code to make up a new stronger encrypted password. If the user forgets their strong password they can re-enter their simple password to retrieve it.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;require 'open-uri'
&lt;br /&gt;require 'rexml/document'
&lt;br /&gt;
&lt;br /&gt;class PwLookup
&lt;br /&gt;  include REXML
&lt;br /&gt;
&lt;br /&gt;  def lookup(s)
&lt;br /&gt;    d = Document.new(open('http://rorbuilder.info/pl/codes', "UserAgent" =&gt; "rcscript").read)
&lt;br /&gt;    s.split(//).map {|c| d.root.elements["code[@index='#{c}']/attribute::value"]}.join
&lt;br /&gt;  end
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;if ARGV[0][/\w+/] then
&lt;br /&gt;  s = ARGV[0]
&lt;br /&gt;  pl = PwLookup.new
&lt;br /&gt;  pw = pl.lookup(s)
&lt;br /&gt;  puts 'your password is ' + pw
&lt;br /&gt;else
&lt;br /&gt;  puts 'supply a simple keyword to convert to a password'
&lt;br /&gt;end
&lt;br /&gt;#=&gt; apple translates to 4h55QfuJ
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;rcscript usage:
&lt;br /&gt;&lt;code&gt;rcscript //job:password_lookup http://rorbuilder.info/r/utility.rsf apple
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Resources:
&lt;br /&gt; - &lt;a href="http://snippets.dzone.com/posts/show/5104"&gt;Encode simple passwords&lt;/a&gt; [dzone.com]
&lt;br /&gt; - &lt;a href="http://rorbuilder.info/r/utility.xml#password_lookup"&gt;utility.xml&lt;/a&gt; [rorbuilder.info]&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/128HOjP5xBg" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/128HOjP5xBg/7536</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/128HOjP5xBg/7536?</guid>
	<pubDate>Thu, 02 Jul 2009 04:51 GMT</pubDate>

</item>

<item>
	<title>Use Ruby Scripting File (RSF) to manage your files</title>
	<description>This script is intended to be a lightweight utility for managing your Ruby scripts using XML.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;#!/usr/bin/ruby
&lt;br /&gt;#file: rcscript.rb
&lt;br /&gt;
&lt;br /&gt;# created: 1-Jul-2009
&lt;br /&gt;
&lt;br /&gt;# description:
&lt;br /&gt;#  - This script executes Ruby script contained within an XML file.
&lt;br /&gt;#  - The XML file can be stored locally or on a website.
&lt;br /&gt;#  - The 'require' statement for foreign scripts has been replaced 
&lt;br /&gt;#      with the script tag (e.g. &lt;script src="rexml_helper.rb"/&gt;)
&lt;br /&gt;
&lt;br /&gt;# arguments:
&lt;br /&gt;# - *job, the filename, and the *script arguments (* (asterisk) denotes 
&lt;br /&gt;#     these arguments  are optional.
&lt;br /&gt;
&lt;br /&gt;# usage:
&lt;br /&gt;# - rcscript //job:xxxx script_file.rsf [script arguments]
&lt;br /&gt;
&lt;br /&gt;# MIT license - basically you can do anything you like with the script.
&lt;br /&gt;#  http://www.opensource.org/licenses/mit-license.php
&lt;br /&gt;
&lt;br /&gt;require 'open-uri'
&lt;br /&gt;require 'rexml/document'
&lt;br /&gt;include REXML
&lt;br /&gt;
&lt;br /&gt;class RScript
&lt;br /&gt;
&lt;br /&gt;  def initialize()
&lt;br /&gt;  end
&lt;br /&gt;  
&lt;br /&gt;  def run
&lt;br /&gt;      
&lt;br /&gt;    if ARGV.to_s[/\/\/job:/] then 
&lt;br /&gt;
&lt;br /&gt;      ajob = []
&lt;br /&gt;      
&lt;br /&gt;      ARGV.each_index do |i| 
&lt;br /&gt;        if ARGV[i][/\/\/job:/] then          
&lt;br /&gt;          ajob &lt;&lt; "@id='#{$'}'"; ARGV[i] = nil
&lt;br /&gt;        end
&lt;br /&gt;      end
&lt;br /&gt;
&lt;br /&gt;      ARGV.compact!
&lt;br /&gt;
&lt;br /&gt;      run_rsf() do |doc| 
&lt;br /&gt;        doc.root.elements.each("//job[#{ajob.join(' or ')}]") do |job|
&lt;br /&gt;          job.elements.each('script') {|s| run_script(s)}    
&lt;br /&gt;        end
&lt;br /&gt;      end
&lt;br /&gt;      
&lt;br /&gt;    else    
&lt;br /&gt;      run_rsf() {|doc| doc.root.elements.each('//script') {|s| run_script(s)}}    
&lt;br /&gt;    end 
&lt;br /&gt;    
&lt;br /&gt;  end
&lt;br /&gt;  
&lt;br /&gt;  private
&lt;br /&gt;  
&lt;br /&gt;  def run_script(script)  
&lt;br /&gt;    src = script.attribute('src')
&lt;br /&gt;    if src then
&lt;br /&gt;      rbfile = script.attribute('src').value
&lt;br /&gt;      eval(read_sourcecode(rbfile))
&lt;br /&gt;    else
&lt;br /&gt;      eval(script.text)
&lt;br /&gt;    end
&lt;br /&gt;  end
&lt;br /&gt;      
&lt;br /&gt;  def run_rsf()
&lt;br /&gt;    rsfile = ARGV[0]; ARGV.shift
&lt;br /&gt;    doc = Document.new(read_sourcecode(rsfile))
&lt;br /&gt;    yield(doc)
&lt;br /&gt;  end
&lt;br /&gt;  
&lt;br /&gt;  def read_sourcecode(rsf)
&lt;br /&gt;    if rsf[/http:\/\//] then
&lt;br /&gt;      return open(rsf, "UserAgent" =&gt; "Ruby-SourceCodeReader").read
&lt;br /&gt;    else
&lt;br /&gt;      return File.open(rsf,'r').read
&lt;br /&gt;    end
&lt;br /&gt;  end          
&lt;br /&gt;  
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;rs = RScript.new()
&lt;br /&gt;rs.run
&lt;br /&gt;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Note: This afternoon I was attempting to replace filenames within a directory and I knew there was a  simple script somewhere that did this I just could not find it as quickly as I would have liked.  Rcscript should  make it easier to organise the hundreds of scripts that are accumulating on my computer.
&lt;br /&gt;
&lt;br /&gt;Here's a sample Ruby Scripting File (welcome.rsf):
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;&lt;package&gt;
&lt;br /&gt;  &lt;job id='hello'&gt;
&lt;br /&gt;    &lt;script src="fun.rb"&gt;
&lt;br /&gt;      puts 'hello world'
&lt;br /&gt;    &lt;/script&gt;
&lt;br /&gt;    &lt;script&gt;
&lt;br /&gt;      puts 'hello me'
&lt;br /&gt;    &lt;/script&gt;
&lt;br /&gt;  &lt;/job&gt;
&lt;br /&gt;  &lt;job id='thanks'&gt;
&lt;br /&gt;    &lt;script&gt;
&lt;br /&gt;      puts 'you are most kind'
&lt;br /&gt;    &lt;/script&gt;
&lt;br /&gt;    &lt;script&gt;
&lt;br /&gt;      puts 'thank you'
&lt;br /&gt;    &lt;/script&gt;
&lt;br /&gt;  &lt;/job&gt;
&lt;br /&gt;  &lt;job id='payment'&gt;
&lt;br /&gt;    &lt;script&gt;
&lt;br /&gt;      puts 'how much is this?'
&lt;br /&gt;    &lt;/script&gt;
&lt;br /&gt;  &lt;/job&gt;
&lt;br /&gt;&lt;/package&gt;
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;file: fun.rb
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;#!/usr/bin/ruby
&lt;br /&gt;
&lt;br /&gt;puts 'juice'
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;Example:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;james@dovellia:~/learning/ruby$ rcscript welcome.rsf //job:hello
&lt;br /&gt;juice
&lt;br /&gt;hello me
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;To run the utility similar to an executable file I sym linked the rcscript.rb file to /usr/bin
&lt;br /&gt;
&lt;br /&gt;Resources:
&lt;br /&gt; - &lt;a href="http://blogs.msdn.com/gstemp/archive/2004/02/24/79183.aspx"&gt;The Scripting Guys' First Blog : How Come You Guys Don't Use .WSF Files?&lt;/a&gt; [msdn.com]
&lt;br /&gt; - &lt;a href="http://www.herongyang.com/VBScript/WSH-WSF-Windows-Script-File-XML-Format.html"&gt;WSF - Windows Script File XML Format&lt;/a&gt; [herongyang.com]&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/uXMGdgEQWfY" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/uXMGdgEQWfY/7533</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/uXMGdgEQWfY/7533?</guid>
	<pubDate>Wed, 01 Jul 2009 15:46 GMT</pubDate>

</item>

<item>
	<title>Find out what's on Radio 4 now</title>
	<description>&lt;code&gt;
&lt;br /&gt;require 'open-uri'
&lt;br /&gt;
&lt;br /&gt;base_url = 'http://www.bbc.co.uk'
&lt;br /&gt;buffer = open(base_url + '/radio4', "UserAgent" =&gt; "Ruby-reader").read
&lt;br /&gt;e = %Q(id="js-rotate-title"&gt;&lt;a href="([^"]+)"&gt;([^&lt;]+)&lt;\/a&gt;&lt;/p&gt;)
&lt;br /&gt;href, title = buffer.match(/#{e}/).captures
&lt;br /&gt;puts title + ' is on Radio 4 now - ' + base_url + href
&lt;br /&gt;
&lt;br /&gt;#=&gt; The Archers is on Radio 4 now - http://www.bbc.co.uk/programmes/b00l9r7d
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Here's a service that simply returns &lt;a href="http://rorbuilder.info/cgi-bin/radio4.cgi"&gt;what's on Radio 4 as XML&lt;/a&gt; [rorbuilder.info].
&lt;br /&gt;
&lt;br /&gt;output:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;&lt;result&gt;
&lt;br /&gt;  &lt;title&gt;Afternoon Play&lt;/title&gt;
&lt;br /&gt;  &lt;link&gt;http://www.bbc.co.uk/programmes/b00lg4c7&lt;/link&gt;
&lt;br /&gt;  &lt;description&gt;Afternoon Play is on Radio 4 now - http://www.bbc.co.uk/programmes/b00lg4c7&lt;/description&gt;
&lt;br /&gt;&lt;/result&gt;
&lt;br /&gt;&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/PuhyZ7e673Y" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/PuhyZ7e673Y/7532</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/PuhyZ7e673Y/7532?</guid>
	<pubDate>Wed, 01 Jul 2009 07:31 GMT</pubDate>

</item>

<item>
	<title>Increment a string with succ</title>
	<description>&lt;code&gt;
&lt;br /&gt;u = '2'
&lt;br /&gt;u.succ!
&lt;br /&gt;puts u
&lt;br /&gt;#=&gt; "3"
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;Resources:
&lt;br /&gt; - &lt;a href="http://www.ruby-doc.org/core/classes/String.html#M000795"&gt;Class: String&lt;/a [ruby-doc.org]&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/y5uPsXprwqg" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/y5uPsXprwqg/7531</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/y5uPsXprwqg/7531?</guid>
	<pubDate>Wed, 01 Jul 2009 06:50 GMT</pubDate>

</item>

<item>
	<title>Use tmail and IMAP to save attachments</title>
	<description>Hope somebody finds this useful
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;#! /usr/bin/ruby
&lt;br /&gt;
&lt;br /&gt;require 'net/imap'
&lt;br /&gt;  # This is a convenience monkey patch
&lt;br /&gt;  class Net::IMAP
&lt;br /&gt;    def uid_move(uid, mailbox)
&lt;br /&gt;      uid_copy(uid, mailbox)
&lt;br /&gt;      uid_store(uid, "+FLAGS", [:Deleted])
&lt;br /&gt;    end
&lt;br /&gt;  end
&lt;br /&gt;require 'rubygems'
&lt;br /&gt;require 'tmail'
&lt;br /&gt;
&lt;br /&gt;imap = Net::IMAP.new('mail.example.com')
&lt;br /&gt;imap.login("user", "password")
&lt;br /&gt;imap.select('Inbox')
&lt;br /&gt;
&lt;br /&gt;imap.uid_search(["SUBJECT", "order mail"]).each do |uid|
&lt;br /&gt;  # save_attachment
&lt;br /&gt;  mail = TMail::Mail.parse( imap.uid_fetch(uid, 'RFC822').first.attr['RFC822'] )
&lt;br /&gt;  if ! mail.attachments.blank?
&lt;br /&gt;    File.open(mail.attachments.first.original_filename,"w+") { |local_file|
&lt;br /&gt;      local_file &lt;&lt; mail.attachments.first.gets(nil)
&lt;br /&gt;    }
&lt;br /&gt;  end
&lt;br /&gt;
&lt;br /&gt;  # archive mail to mailbox
&lt;br /&gt;  imap.uid_move(uid, 'Inbox.archived')
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;imap.expunge
&lt;br /&gt;imap.logout
&lt;br /&gt;&lt;/code&gt;&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/GlciXCP_gOM" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/GlciXCP_gOM/7530</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/GlciXCP_gOM/7530?</guid>
	<pubDate>Wed, 01 Jul 2009 05:52 GMT</pubDate>

</item>

<item>
	<title>Use a hash as a parameter list</title>
	<description>Source: &lt;a href="http://ruby.about.com/od/advancedruby/qt/opthash.htm"&gt;Optional Method Parameters with Hashes&lt;/a&gt; [about.com]
&lt;br /&gt;
&lt;br /&gt;&lt;snip&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;    def my_method(opts={})
&lt;br /&gt;      o = {
&lt;br /&gt;        :a =&gt; 'Testing',
&lt;br /&gt;        :b =&gt; 'this',
&lt;br /&gt;        :c =&gt; 'feature'
&lt;br /&gt;      }.merge(opts)
&lt;br /&gt;      
&lt;br /&gt;      puts "#{o[:a]} #{o[:b]} #{o[:c]}"
&lt;br /&gt;    end
&lt;br /&gt;
&lt;br /&gt;    my_method
&lt;br /&gt;    my_method( :b =&gt; 'this great' )
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;While it is a bit more verbose to access the parameters, it is much more flexible.
&lt;br /&gt;&lt;/snip&gt;
&lt;br /&gt;
&lt;br /&gt;output:
&lt;br /&gt;&lt;pre&gt;Testing this great feature&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;What is special about this snippet is not just that it uses a hash but it uses default values for parameters (in a hash) which are then overridden by the actual parameters (in a hash) using 'merge'.
&lt;br /&gt;
&lt;br /&gt;*update @ 10:13 1-Jul-09
&lt;br /&gt;How I actually intended to use this was to first of all use XML as the parameter list e.g.
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;  def params_to_h(params)
&lt;br /&gt;    h = {}
&lt;br /&gt;    doc = Document.new(params).root.elements.each("param[@var]") do |param|
&lt;br /&gt;      var = param.attribute('var').to_s.to_sym
&lt;br /&gt;      h[var] = param.children.to_s 
&lt;br /&gt;    end
&lt;br /&gt;    h
&lt;br /&gt;  end
&lt;br /&gt;
&lt;br /&gt;  def start(params)
&lt;br /&gt;    h = params_to_h(params)
&lt;br /&gt;    o = {:ready =&gt; 'false', :status =&gt; 'do not move', :conditions =&gt; 'good'}.merge h
&lt;br /&gt;    puts o.inspect
&lt;br /&gt;  end
&lt;br /&gt;
&lt;br /&gt;  start("&lt;params&gt;&lt;param var='ready'&gt;true&lt;/param&gt;&lt;param var='status'&gt;go&lt;/param&gt;&lt;/params&gt;")
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;output:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;{:ready =&gt; 'true', :status =&gt; 'go', :conditions =&gt; 'good'}
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;Note: merge must be affixed to the end of the newly created hash otherwise the defaults will not be overwritten unless you use merge!.&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/W0fphRLQQo0" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/W0fphRLQQo0/7529</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/W0fphRLQQo0/7529?</guid>
	<pubDate>Wed, 01 Jul 2009 02:55 GMT</pubDate>

</item>

<item>
	<title>Use lambda for strict argument checking</title>
	<description>Source: &lt;a href="http://www.humblelittlerubybook.com/book/html/chapter2.html"&gt;Mr. Neighborly's Humble Little Ruby Book&lt;/a&gt; [humblelittlerubybook.com]
&lt;br /&gt;
&lt;br /&gt;&lt;snip&gt;
&lt;br /&gt; First of all, Proc objects created with lambda have stricter argument checking than those created with Proc.new.
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;lproc = lambda {|a,b| puts "#{a + b} &lt;- the sum"}
&lt;br /&gt;nproc = Proc.new {|a,b| puts "#{a + b} &lt;- the sum"}
&lt;br /&gt;
&lt;br /&gt;nproc.call(1, 2, 3)
&lt;br /&gt;#→ 3
&lt;br /&gt;
&lt;br /&gt;lproc.call(1, 2, 3)
&lt;br /&gt;#→ !ArgumentError (wrong number of arguments (3 for 2))
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;&lt;/snip&gt;
&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/6yWHHXGWAS4" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/6yWHHXGWAS4/7527</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/6yWHHXGWAS4/7527?</guid>
	<pubDate>Tue, 30 Jun 2009 11:56 GMT</pubDate>

</item>

<item>
	<title>Use alias to give a method a different name</title>
	<description>Source: &lt;a href="http://www.ruby-doc.org/docs/keywords/1.9/files/ruby_1_9_keywords_rb.html#M000006"&gt;File: ruby_1.9_keywords.rb [RDoc Documentation]&lt;/a&gt; [ruby-doc.org]
&lt;br /&gt;
&lt;br /&gt;I've copied this example here because 'alias' is a helpful method for perhaps polishing your code. It makes calling a method from a programmer's perspective less of a surprise depending on their style of coding i.e. I prefer using array#map instead of array#collect even though they are the same method.
&lt;br /&gt;
&lt;br /&gt;&lt;snip&gt;
&lt;br /&gt;  alias()
&lt;br /&gt;
&lt;br /&gt;Creates an alias or duplicate method name for a given method. The original method continues to be accessible via the alias, even if it is overriden. Takes two method-name arguments (which can be represented by strings or symbols but can also be the bare names themselves).
&lt;br /&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;
&lt;br /&gt;  class Person
&lt;br /&gt;    def name=(name)
&lt;br /&gt;      puts "Naming your person #{name}!"
&lt;br /&gt;      @name = name
&lt;br /&gt;    end
&lt;br /&gt;
&lt;br /&gt;    alias full_name= name=
&lt;br /&gt;  end
&lt;br /&gt;
&lt;br /&gt;  p = Person.new
&lt;br /&gt;  p.name = "David"        # Naming your person David!
&lt;br /&gt;
&lt;br /&gt;  class Person
&lt;br /&gt;    def name=(name)
&lt;br /&gt;      puts "Please use full_name="
&lt;br /&gt;    end
&lt;br /&gt;  end
&lt;br /&gt;
&lt;br /&gt;  p.full_name = "David"   # Please use fullname=
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;&lt;/snip&gt;&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/uWl9Hkh8g10" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/uWl9Hkh8g10/7526</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/uWl9Hkh8g10/7526?</guid>
	<pubDate>Tue, 30 Jun 2009 09:26 GMT</pubDate>

</item>

<item>
	<title>Use thread.join to ensure proper thread execution</title>
	<description>Source: &lt;a href="http://www.rubycentral.com/pickaxe/tut_threads.html"&gt;Programming Ruby: The Pragmatic Programmer's Guide&lt;/a&gt; [rubycentral.com]
&lt;br /&gt;
&lt;br /&gt;Prior to using thread.join I had observed threads being executed and then terminated early as a result of the main script finishing. The main script does not wait on threads to finish unless threads use something like the following: 
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;threads.each { |aThread|  aThread.join }
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;e.g.
&lt;br /&gt;&lt;snip&gt;
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;require 'net/http'
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;pages = %w( www.rubycentral.com
&lt;br /&gt;            www.awl.com
&lt;br /&gt;            www.pragmaticprogrammer.com
&lt;br /&gt;           )
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;threads = []
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;for page in pages
&lt;br /&gt;  threads &lt;&lt; Thread.new(page) { |myPage|
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;    h = Net::HTTP.new(myPage, 80)
&lt;br /&gt;    puts "Fetching: #{myPage}"
&lt;br /&gt;    resp, data = h.get('/', nil )
&lt;br /&gt;    puts "Got #{myPage}:  #{resp.message}"
&lt;br /&gt;  }
&lt;br /&gt;end
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;threads.each { |aThread|  aThread.join }
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;produces:
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;Fetching: www.rubycentral.com
&lt;br /&gt;Fetching: www.awl.com
&lt;br /&gt;Fetching: www.pragmaticprogrammer.com
&lt;br /&gt;Got www.rubycentral.com:  OK
&lt;br /&gt;Got www.pragmaticprogrammer.com:  OK
&lt;br /&gt;Got www.awl.com:  OK
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;... Why do we call join on each of the threads we created?
&lt;br /&gt;
&lt;br /&gt;When a Ruby program terminates, all running threads are killed, regardless of their states. However, you can wait for a particular thread to finish by calling that thread's Thread#join method. The calling thread will block until the given thread is finished. By calling join on each of the requestor threads, you can make sure that all three requests have completed before you terminate the main program.
&lt;br /&gt;&lt;/snip&gt;
&lt;br /&gt;
&lt;br /&gt;Resources:
&lt;br /&gt; - &lt;a href="http://stackoverflow.com/questions/1053232/derived-class-for-ruby-thread"&gt;Derived class for Ruby Thread? - Stack Overflow&lt;/a&gt; [stackoverflow.com] - this post helped my interest in finding out more on thread.join
&lt;br /&gt; - &lt;a href="http://www.ruby-doc.org/core/classes/Thread.html"&gt;Class: Thread&lt;/a&gt; [ruby-doc.org]
&lt;br /&gt;
&lt;br /&gt;*update @ 15:00 30-Jun-09*
&lt;br /&gt;While looking through the thread docmentation I found the following snippet to be helpful
&lt;br /&gt;&lt;code&gt;
&lt;br /&gt;  y = Thread.new { 4.times { sleep 0.1; puts 'tick... ' }}
&lt;br /&gt;  puts "Waiting" until y.join(0.15)
&lt;br /&gt;&lt;/code&gt;
&lt;br /&gt;
&lt;br /&gt;output
&lt;br /&gt;&lt;pre&gt;
&lt;br /&gt;tick... 
&lt;br /&gt;Waiting
&lt;br /&gt;tick... 
&lt;br /&gt;Waiting
&lt;br /&gt;tick... 
&lt;br /&gt;tick... 
&lt;br /&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;It demonstrates that the statement following the thread can be influenced by the thread's timer event.&lt;img src="http://feeds.feedburner.com/~r/snippets/ruby/~4/BhgeP-HBNpQ" height="1" width="1"/&gt;</description>
	<link>http://feeds.dzone.com/~r/snippets/ruby/~3/BhgeP-HBNpQ/7525</link>
	<source url="http://www.bigbold.com/snippets/rss/tag/ruby">Code Snippets: ruby code</source>
	<guid isPermaLink="false">http://feeds.dzone.com/~r/snippets/ruby/~3/BhgeP-HBNpQ/7525?</guid>
	<pubDate>Tue, 30 Jun 2009 07:39 GMT</pubDate>

</item>


</channel></rss>

