On Fri, May 22, 2009 at 08:10:09PM +0900, Balint Erdi wrote:
> Hi,
> 
> Deleting (vs. just fetching) the hash key from an options hash that
> was passed in as an argument to the method seems prevalent. I saw it
> in several high-quality OS projects (e.g DataMapper, Rails)
> 
> For example:
> 
> unless options.delete(:only_path)
>         url << (options.delete(:protocol) || 'http')
>         url << '://' unless url.match("://")
>         ...
> end

This snippset let you test on "mispelled" or "extra" opts:

Example:

def initialize(opts)
  @a = opts.delete(:a) || raise("missing :a")
  @b = opts.delete(:b) || "b"
  raise "unkown options: %s" % opts.keys.inspect unless opts.empty?
end


> 
> I wonder what advantage the above snippet has instead of writing:
> 
> unless options[:only_path]
>         url << (options[:protocol] || 'http')
>         url << '://' unless url.match("://")
>         ...
> end
> 
> Thank you,
> Balint

Markus