如何将键和值推入空的散列瓦特/红宝石? [英] How to push keys and values into an empty hash w/ Ruby?
问题描述
我有一个字典类,希望能够使用'add'方法将键(作为关键字)和值(作为定义)推送到空的散列中。我不明白如何在语法上编写它。
红宝石:
我也包含了一个RSPEC文件。 class Dictionary
attr_accessor:关键字,定义
def entries
@hash = {}
end
def add(options)
options.map do | keyword,definition |
@hash [keyword.to_sym] =定义
结束
结束
结束
Rspec
require'dictionary'
在do
@d = Dictionary.new
end
之前描述Dictionary do
它可以用关键字和定义添加整个条目'do
@ d.add ('鱼'=>'水生动物')
@ d.entries.should == {'fish'=> 'aquatic animal'}
@ d.keywords.should == ['fish']
end
任何帮助表示赞赏。谢谢!
更新:
谢谢戴夫牛顿回复。我使用了你的代码,并得到这个错误:
错误:
*失败/错误:@ d.keywords.should == ['fish']
NoMethodError:
未定义方法关键字for#<字典:0x007fb0c31bd458
@hash = {fish=>aquatic animal}> *
当我使用@hash [word.to_sym] = defintion
*将'word'转换为符号时出现不同的错误* Failure / Error :@ d.entries.should == {'fish'=> 'aquatic animal'}
expected:{fish=>aquatic animal}
got:{:fish =>aquatic animal}(使用==)
Diff :
@@ -1,2 +1,2 @@
- fish=> 水生动物
+:鱼=> 水生动物*
c $ c> Dictionary 的初始化
:
class Dictionary
def initialize
@hash = {}
end
def add(defs)
defs.each do | word,definition |
@hash [word] = definition
end
end
end
现在,除非您调用条目
,否则没有散列。
条目
应该返回现有的哈希,而不是创建一个新的哈希。
关键字
应该返回哈希键。
您不需要关键字
和定义
。这样的单项在词典课上没有意义。您可能需要像 lookup(word)
这样的返回定义。
另外,您将单词转换为符号,但我不知道为什么–特别是因为您在规范中使用了字符串键。选择一个,虽然我不相信这是符号增加价值的情况。
请仔细考虑变量命名以提供尽可能多的上下文。
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"*
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.
这篇关于如何将键和值推入空的散列瓦特/红宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!