如何使用 ruby​​ 从给定数组中生成或创建最大数

Ami*_*rma 0 ruby ruby-on-rails

我想使用 Ruby 从给定的数组中生成最大的数字。

示例 1

## Input
my_array = %w{8 40 9}

## Expected Output
"9840"
Run Code Online (Sandbox Code Playgroud)

示例 2

## Input
my_array = %w{9 8 40 9}

## Expected Output
"99840"
Run Code Online (Sandbox Code Playgroud)

小智 5

使用sort带有自定义比较块。

my_array.sort{|a, b| b + a <=> a + b }