TheGame
TheGame

Reputation: 111

Convert array Ruby

How can I convert ["90 99 8 9 11 22"] to ["90", "99", "8", "9", "11", "22"] in Ruby ?

Upvotes: 1

Views: 38

Answers (1)

Yakov
Yakov

Reputation: 3201

With flat_map and split it works with any number of items:

["90 99 8 9 11 22"].flat_map(&:split)
=> ["90", "99", "8", "9", "11", "22"]

> ["90 99 8 9 11 22", "1 2 3"].flat_map(&:split)
=> ["90", "99", "8", "9", "11", "22", "1", "2", "3"]

Upvotes: 3

Related Questions