先测试Ruby字典添加方法

Test First Ruby Dictionary Add method

我是 ruby

的新手

@d.add('fish' => 'aquatic animal') @d.find('fish').应该== {'fish' => 'aquatic animal'}

但是你怎么能把 => 作为参数呢?如何获取参数 'fish'=>'aquatic animal' 并设置实例变量 @entries[fish] = aquatic animal?

你 Class 可以看起来像这样:

class Dictionary

  def new
    @entries = {}
  end

  def add(hash = {})
    hash.each do |key, value| 
      @entries.update(key => value)    # or `@entries[key] = value`
    end
  end

  def find(key)
    value = @entries[key]
    { key => value } if value
  end

end