How to “unflatten” a Ruby Array?

前端 未结 5 1517
名媛妹妹
名媛妹妹 2020-12-11 05:39

I am currently trying to convert this ruby array:

[5, 7, 8, 1]

into this:

[[5], [7], [8], [1]]

What\'s th

5条回答
  •  悲哀的现实
    2020-12-11 06:04

    There's nothing specifically wrong with what you're doing. I think they mean that to_a for a FixNum will be deprecated sometime in the future, which makes sense cause it's ambiguous what exactly to_a for a FixNum should do.

    You could rewrite your line like this which would eliminate the error:

    [5, 7, 8, 1].select { |element| element }.collect { |element| [element] }
    

提交回复
热议问题