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 syntac
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")