I have a Ruby programming task. My code passes all the rspec requirements, but I am not sure if I've done it in the right way. And I'm not quite sure why I need :entries there...
Here is the rspec test:
# # Topics
#
# * Hash
# * Array
# * instance variables
# * regular expressions
require '11_dictionary'
describe Dictionary do
before do
@d = Dictionary.new
end
it "is empty when created" do
expect(@d.entries).to eq({})
end
it "can add whole entries with keyword and definition" do
@d.add("fish" => "aquatic animal")
expect(@d.entries).to eq({"fish" => "aquatic animal"})
expect(@d.keywords).to eq(["fish"])
end
it "add keywords (without definition)" do
@d.add("fish")
expect(@d.entries).to eq({"fish" => nil})
expect(@d.keywords).to eq(["fish"])
end
it "can check whether a given keyword exists" do
expect(@d.include?("fish")).to be_falsey
end
it "doesn't cheat when checking whether a given keyword exists" do
expect(@d.include?("fish")).to be_falsey # if the method is empty, this test passes with nil returned
@d.add("fish")
expect(@d.include?("fish")).to be_truthy # confirms that it actually checks
expect(@d.include?("bird")).to be_falsey # confirms not always returning true after add
end
it "doesn't include a prefix that wasn't added as a word in and of itself" do
@d.add("fish")
expect(@d.include?("fi")).to be_falsey
end
it "doesn't find a word in empty dictionary" do
expect(@d.find("fi")).to be_empty # {}
end
it "finds nothing if the prefix matches nothing" do
@d.add("fiend")
@d.add("great")
expect(@d.find("nothing")).to be_empty
end
it "finds an entry" do
@d.add("fish" => "aquatic animal")
expect(@d.find("fish")).to eq({"fish" => "aquatic animal"})
end
it "finds multiple matches from a prefix and returns the entire entry (keyword + definition)" do
@d.add("fish" => "aquatic animal")
@d.add("fiend" => "wicked person")
@d.add("great" => "remarkable")
expect(@d.find("fi")).to eq({"fish" => "aquatic animal", "fiend" => "wicked person"})
end
it "lists keywords alphabetically" do
@d.add("zebra" => "African land animal with stripes")
@d.add("fish" => "aquatic animal")
@d.add("apple" => "fruit")
expect(@d.keywords).to eq(%w(apple fish zebra))
end
it "can produce printable output like so: [keyword] 'definition'" do
@d.add("zebra" => "African land animal with stripes")
@d.add("fish" => "aquatic animal")
@d.add("apple" => "fruit")
expect(@d.printable).to eq(%Q{[apple] "fruit"\n[fish] "aquatic animal"\n[zebra] "African land animal with stripes"})
end
end
Here is my code:
class Dictionary
attr_accessor :entries
def initialize
@hash={}
end
def add(a)
if a.class == Hash
a.each_pair { |k, v|
@hash[k]=v
}
else
@hash[a]=nil
end
end
def entries
@hash
end
def keywords
@hash.keys.sort
end
def include?(k)
@hash.key?(k)
end
def printable
str=[]
[email protected]
key.each do |el|
@hash.each_pair do |k,v|
if k==el
str << "[#{k}] \"#{v}\""
end
end
end
return str.join("\n")
end
def find(str)
if str.class == Hash
str.each_pair { |k, v|
return (@hash.select { |k,v| k.include?(k)})
}
elsif str.class == String
return (@hash.select {|k,v| k.include?(str)})
end
end
end