From: "Wai-Sun Chia" <waisun.chia / hp.com>


> Why doesn't this work?
> ==================== ha.rb ======================
> #!/usr/bin/ruby
> class Song
>        def initialize(name, artist, duration)
>            @name     = name
>            @artist   = artist
>            @duration = duration
>      end
> end
>
> a = Song.new("name"=>"foo", "artist"=>"bar", "duration"=>3)
>
> Wai-Sun "Squidster" Chia

Pretty simple, really.  It's not valid Ruby to use named arguments.  You've
actually sent Song.new a Hash with three entries, whereas the method expects
three arguments.

Try this:

class Song
  def initialize(args)
    @name, @artist, @duration = args.indices(:name, :artist, :duration)
  end
end

a = Song.new(:name => "foo", :artist => "bar", :duration => 3)

Aside: I make this comment cautiously, because it could just be my mail client,
but it appears you've used an existing thread to ask a new question, whereas it
belongs in a new thread.  If this is true, please use "New" instead of "Reply"
when introducing a new question.

Gavin