程序将命令行的输入转换为数组并找到其中最大的一个

Aay*_*ush 1 ruby ruby-1.9.3

我是Ruby的新手,无法弄清楚如何从用户那里获取数组的输入并显示它.如果有人能够清楚我可以添加我的逻辑来找到最大的数字.

#!/usr/bin/ruby

puts "Enter the size of the array"
n = gets.chomp.to_i
puts "enter the array elements"
variable1=Array.new(n)

for i in (0..n)
  variable1[i]=gets.chomp.to_i
end

for i in (0..n)
  puts variable1
end  
Run Code Online (Sandbox Code Playgroud)

Tan*_*ili 6

如何在一行中捕获数组?

#!/usr/bin/ruby

puts "Enter a list of numbers"

list = gets   # Input something like "1 2 3 4" or "3, 5, 6, 1"

max = list.split.map(&:to_i).max

puts "The largest number is: #{max}"
Run Code Online (Sandbox Code Playgroud)