7

Stuck on a Code Wars Challenge: Complete the solution so that it takes an array of keys and a default value and returns a hash with all keys set to the default value.

My answer results in a parse error:

def solution([:keys, :default_value])
  return { :keys => " ", :default_value => " " }
end

Am I missing something to do with returning a hash key with all the keys set to the default value?

3
  • While I prefer Arup's solution, the "initialize a collection-add to it-return it" pattern is handled by Enumerable#inject in Ruby: keys.inject({}) { |h, k| h.merge({k => default_val}) } or keys.inject({}) { |h, k| h[k] = default_val ; h }. Commented Jan 12, 2014 at 16:24
  • @Michael, to get rid of that unsightly h at the end of the block: keys.each_with_object({}) { |k, h| h[k] = default_val }. Commented Jan 12, 2014 at 19:25
  • To replace it with an unsightly method name? I'd rather not. Commented Jan 13, 2014 at 0:54

2 Answers 2

15

Do as below :

def solution(keys,default_val)
  Hash[keys.product([default_val])]
end

solution([:key1,:key2],12)  # => {:key1=>12, :key2=>12}

Read Array#product and Kernel#Hash.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. So using Hash before an array turns that array into a Hash? Can you give me a real world example of where the product method could be used - i mean, why would the same value be assigned to multiple keys? Is there another method i could have used other than product?
@Abraham yes... then do Hash[keys.zip([default_val]* keys.size)].. But in this case it is not idiomatic.. so I use Array#product.
@Abraham For example: you have hash that contain number of letters of in a sentence ( {:a => 0, :b => 0...} ) and you count each letter, using each_char for example, you can do this hash1[:a] += 1. Without default value you have to check if hash contain key, if yes you can hash[:a]+=1, if not you must assign it via hash[:a] = 1. ps. []#* is interesting - it can take string as an argument while []#product will raise TypeError. [1,2,3].* '3' #=> "13233". You forgot to change 3 to Numeric, and you have string instead of array.
This forgets to dup the default value when possible, though.
@Denis Is your advice for me ?
|
1

I'd advise amending your solution to this:

def solution(keys, default_value)
  hash = {}
  keys.each do |key|
    value = default_value.dup rescue default_value
    hash[key] = value
  end
  hash
end

The dup is to work around the nasty case where default_value is a string and you then do e.g.:

hash[:foo] << 'bar'

… with your version, this would modify multiple values in place instead of a single one.

2 Comments

Denis, I am sure you are aware that you could have written keys.each_with_object({}) do |key, hash| to get rid of hash = {} and hash at the end, but chose to do it this way. I'd be interested in your views on the stylistic difference.
I was merely fixing OP's function. Code I'd write myself would be more like Hash[keys.map{ |k| [k, val] }.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.