Reputation: 15691
There is a hash {'results' => [], 'snow' => [], 'ham' => [], 'plow' => [] }
and I want to build a string dynamically (the keys may change), which has all the keys, excluding "results", which looks like this "snow + ham + plow"
How do i do this?
Upvotes: 1
Views: 161
Reputation: 110695
Two other ways (#1 being my preference):
h = {'results' => [], 'snow' => [], 'ham' => [], 'plow' => [] }
#1
(h.keys - ['results']).join(' + ') #=> "snow + ham + plow"
#2
a = h.keys
a.delete('results')
a.join(' + ') #=> "snow + ham + plow"
Upvotes: 3
Reputation: 176733
Use Hash#keys
to get the keys, Array#reject
to reject the "results" one, and Array#join
to join them into a String:
hash.keys.reject { |k| k == "results" }.join(" + ")
Upvotes: 3