From: Joel VanderWerf Date: 2002-10-03T13:06:35+09:00 Subject: Re: Named Arguments doesn't work? Wai-Sun Chia wrote: > 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) > puts a.inspect > ================================================= Try this: class Song def initialize(arg) @name = arg[:name] @artist = arg[:artist] @duration = arg[:duration] end end a = Song.new(:name=>"foo", :artist=>"bar", :duration=>3) puts a.inspect I changed "name" to :name and so on because comparison and hashing are faster for symbols than for strings.