From: "Pedro Del Gallego" <pedro.delgallego / gmail.com>
Subject: Methods and :parameters
Date: Mon, 26 Feb 2007 10:57:59 +0900
Message-ID: <2e93189b0702251757p34aca575v4b8bc41ad115d330 / mail.gmail.com>
> My question is how can i wirte a method with several optional
> parameter and then acces they inside of the mehotd ?

For example, 
-------------------------------------------------------------------
class Book
  def initialize(properties = {})
    @props = {
      'title'  => '',
      'author' => '',
      'tag'    => nil,
      'price'  => nil,
      'ISBN'   => nil,
    }

    properties.each{|k, v|
      k = k.to_s
      raise ArgumentError, "unknown property: #{k}" unless @props.key?(k)
      @props[k] = v
    }
  end

  def method_missing(id, *args)
    prop = id.id2name
    case args.length
    when 1
      if prop[-1] == ?=
        self[prop[0..-2]] = args[0]
        args[0]
      else
        self[prop] = args[0]
        self
      end
    when 0
      self[prop]
    else
      super(id, *args)
    end
  end

  def [](prop)
    prop = prop.to_s
    raise ArgumentError, "unknown property: #{prop}" unless @props.key?(prop)
    @props[prop]
  end

  def []=(prop, val)
    prop = prop.to_s
    raise ArgumentError, "unknown property: #{prop}" unless @props.key?(prop)
    @props[prop] = val
  end
end

def add_book(props)
  book = Book.new(props)
  puts "case1: title : #{book.title} -- Author : #{book.author}"
  puts "case2: title : #{book[:title]} -- Author : #{book[:author]}"
  puts "case3: title : #{book['title']} -- Author : #{book['author']}"
  book
end

add_book :title=>"El quijote", :author=> "Miguel de Cervantes"
add_book :title=>"El quijote", :author=> "Miguel de Cervantes", :tag=>"novel"
add_book :title=>"El quijote"

add_book :title=>"hogehoge", :language=>"Japanese"  # => raise ArgumentError
-------------------------------------------------------------------
-- 
Hidetoshi NAGAI (nagai / ai.kyutech.ac.jp)