How come I can't set a key value pair as a variable, but I can set it as a parameter?

StackOverflow https://stackoverflow.com/questions/18134200

  •  24-06-2022
  •  | 
  •  

سؤال

Working on dictionary lesson from Test First Teaching

In my dictionary.rb file I've got

def add(hash)
  @new = Hash[hash]
  @entries.merge!(@new)
end 

My spec file uses add like this: @d.add('fish' => 'aquatic animal') Which works.

However, when I'm in pry if i set: a = 'monkeys' => '3' I get SyntaxError: unexpected =>, expecting end-of-input a = 'monkeys' => '3'

What gives?

هل كانت مفيدة؟

المحلول

You cannot omit the braces {} of a hash literal in that environment. Where you can do it is within arguments, arrays, etc.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top