Reputation: 1842
@ dictionary = {"cat"=>"Sam"}
This a return a key
@dictionary.key(x)
This returns a value
@dictionary[x]
How do I return the entire element "cat"=>"Sam"
Upvotes: 0
Views: 2382
Reputation: 80075
You can get the key and value in one go - resulting in an array:
@h = {"cat"=>"Sam", "dog"=>"Phil"}
key, value = p h.assoc("cat") # => ["cat", "Sam"]
Use rassoc
to search by value ( .rassoc("Sam")
)
Upvotes: 0
Reputation: 110695
If you wish to return one element of a hash h
, you will need to specify the key to identify the element. As the value for key k
is h[k]
, the key-value pair, expressed as an array, is [k, h[k]]
. If you wish to make that a hash with a single element, use Hash[[[k, h[k]]]]
.
For example, if
h = { "cat"=>"Sam", "dog"=>"Diva" }
and you only wanted to the element with key "cat"
, that would be
["cat", h["cat"]] #=> ["cat", "Sam"]
or
Hash[[["cat", h["cat"]]]] #=> {"cat"=>"Sam"}
With Ruby 2.1 you could alternatively get the hash like this:
[["cat", h["cat"]]].to_h #=> {"cat"=>"Sam"}
Let's look at a little more interesting case. Suppose you have an array arr
containing some or all of the keys of a hash h
. Then you can get all the key-value pairs for those keys by using the methods Enumerable#zip and Hash#values_at:
arr.zip(arr.values_at(*arr))
Suppose, for example,
h = { "cat"=>"Sam", "dog"=>"Diva", "pig"=>"Petunia", "owl"=>"Einstein" }
and
arr = ["dog", "owl"]
Then:
arr.zip(h.values_at(*arr))
#=> [["dog", "Diva"], ["owl", "Einstein"]]
In steps:
a = h.values_at(*arr)
#=> h.values_at(*["dog", "owl"])
#=> h.values_at("dog", "owl")
#=> ["Diva", "Einstein"]
arr.zip(a)
#=> [["dog", "Diva"], ["owl", "Einstein"]]
To instead express as a hash:
Hash[arr.zip(h.values_at(*arr))]
#=> {"dog"=>"Diva", "owl"=>"Einstein"}
Upvotes: 0
Reputation: 9485
Your example is a bit (?) misleading in a sense it only has one pair (while not necessarily), and you want to get one pair. What you call a "dictionary" is actually a hashmap (called a hash among Rubyists).
A hashrocket (=>
) is a part of hash definition syntax. It can't be used outside it. That is, you can't get just one pair without constructing a new hash. So, a new such pair would look as: { key => value }
.
So in order to do that, you'll need a key and a value in context of your code somewhere. And you've specified ways to get both if you have one. If you only have a value, then:
{ @dictionary.key(x) => x }
...and if just a key, then:
{ x => @dictionary[x] }
...but there is no practical need for this. If you want to process each pair in a hash, use an iterator to feed each pair into some code as an argument list:
@dictionary.each do |key, value|
# do stuff with key and value
end
This way a block of code will get each pair in a hash once.
If you want to get not a hash, but pairs of elements it's constructed of, you can convert your hash to an array:
@dictionary.to_a
# => [["cat", "Sam"]]
# Note the double braces! And see below.
# Let's say we have this:
@dictionary2 = { 1 => 2, 3 => 4}
@dictionary2[1]
# => 2
@dictionary2.to_a
# => [[1, 2], [3, 4]]
# Now double braces make sense, huh?
It returns an array of pairs (which are arrays as well) of all elements (keys and values) that your hashmap contains.
Upvotes: 0
Reputation: 11116
@dictionary
should do the trick for you
whatever is the last evaluated expression in ruby is the return value of a method. If you want to return the hash as a whole. the last line of the method should look like the line I have written above
Upvotes: 1