Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
ループして特定の値を別の配列にプッシュしている配列があります。元:
first_array = ["Promoter: 8", "Passive: 7"]
整数であるすべての値を別の配列にプッシュしたいのですが、最終的には次のようになります。
final_array = [8,7]
新しい配列の値が整数であると便利です。文字列内のすべての数値を新しい配列にプッシュする方法は考えられませんが、私が望んでいることを行うための最良のオプションは何でしょうか?
first_array.map{|s| s[/\d+/].to_i} # => [8, 7]
first_array.map{|a| a.match(/\d+/)}.compact.map{|a| a[0].to_i }