All aboard the hash rocket, or maybe not?please help


#1



please help i dunno what is wrong with my code:

movies = Hash.new{
  :primer => "Awesome",
  :memento => "Not as good the 2nd time",
}
here is the problem:
(ruby):1: syntax error, unexpected tASSOC, expecting '}'
  :primer => "Awesome",
            ^
(ruby):1: syntax error, unexpected ',', expecting '}'
(ruby):2: syntax error, unexpected ',', expecting '}'
THANKS


#2

Use Hash literal syntax - delete Hash.new, becase it doesn't take block arguments.
Hash.new accepts a value which will be returned when trying to access non-existent key - default is nil.
Example:

    movies = Hash.new("nothing here")
    movies["somekey"]
    =>"nothing here"

#3

Thanks for your help


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.