How to push keys and values into an empty hash w/ Ruby?

坚强是说给别人听的谎言 提交于 2020-01-01 02:26:08

问题


I have a dictionary class and want to be able to push keys (as keywords) and values (as definitions) into an empty hash with an 'add' method. I don't understand how to syntactically write that. I have included an RSPEC file too.

ruby:

 class Dictionary
     attr_accessor :keyword, :definition
     def entries
          @hash = {}
     end
     def add(options)
          options.map do |keyword, definition|
               @hash[keyword.to_sym] = definition
          end
     end
 end

Rspec:

 require 'dictionary'

   describe Dictionary do
   before do
       @d = Dictionary.new
   end
   it 'can add whole entries with keyword and definition' do
       @d.add('fish' => 'aquatic animal')
       @d.entries.should == {'fish' => 'aquatic animal'}
       @d.keywords.should == ['fish']
   end

Any help is appreciated. Thank you!

UPDATE: Thank you Dave Newton for replying. I used your code and got this error:

ERROR:

 *Failure/Error: @d.keywords.should == ['fish']
 NoMethodError:
 undefined method `keywords' for #<Dictionary:0x007fb0c31bd458 
 @hash={"fish"=>"aquatic animal"}>*

I get a different error when I convert 'word' into a symbol using @hash[word.to_sym] = defintion

 *Failure/Error: @d.entries.should == {'fish' => 'aquatic animal'}
   expected: {"fish"=>"aquatic animal"}
        got: {:fish=>"aquatic animal"} (using ==)
   Diff:
   @@ -1,2 +1,2 @@
   -"fish" => "aquatic animal"
   +:fish => "aquatic animal"*

回答1:


Instantiate your hash in Dictionary's initialize:

class Dictionary
  def initialize
    @hash = {}
  end

  def add(defs)
    defs.each do |word, definition|
      @hash[word] = definition
    end
  end
end

Right now you have no hash until you call entries, which you don't.

entries should return the existing hash, not create a new one.

keywords should return the hash's keys.

You do not need accessors for keyword and definition. Such singular items are meaningless in a dictionary class. You might want something like lookup(word) that returned a definition.

Also, you convert words to symbols, but I don't know why–particularly since you use string keys in your spec. Pick one, although I'm not convinced this is a case where symbols add value.

Please consider variable naming carefully to provide as much context as possible.




回答2:


Looking at your Rspec, It looks like you need this setup.

class Dictionary
  def initialize
    @hash = {}
  end

  def add(key_value)
    key_value.each do |key, value|
      @hash[key] = value
    end
  end

  def entries
     @hash
  end

  def keywords
     @hash.keys
  end

end

Do not use key.to_sym in add method, just key

To give flexibility to add method, I can return the self object to continuesly add.

def add(key_value)
  key_value.each do |key, value|
    @hash[key] = value
  end
  self
end

So, I can do this now.

@d.add("a" => "apple").add("b" => "bat")


来源:https://stackoverflow.com/questions/15034262/how-to-push-keys-and-values-into-an-empty-hash-w-ruby

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!