Ruby Hash to array of values

后端 未结 6 425
太阳男子
太阳男子 2020-12-04 09:32

I have this:

hash  = { \"a\"=>[\"a\", \"b\", \"c\"], \"b\"=>[\"b\", \"c\"] } 

and I want to get to this: [[\"a\",\"b\",\"c\"],[

6条回答
  •  失恋的感觉
    2020-12-04 09:53

    hash.collect { |k, v| v }
    #returns [["a", "b", "c"], ["b", "c"]] 
    

    Enumerable#collect takes a block, and returns an array of the results of running the block once on every element of the enumerable. So this code just ignores the keys and returns an array of all the values.

    The Enumerable module is pretty awesome. Knowing it well can save you lots of time and lots of code.

提交回复
热议问题