我刚开始学习Ruby,正在尝试使用数组。我想要将数组打印在单行上。
以下是代码块(请忽略任何错误):
当我在
有没有办法将输出放在单行中? 例如:
array=[]
puts "Choose an option: ","1.Push, 2.Pop, 3.Display Length"
choice=gets.to_i
while choice!=4
if choice==1
puts "enter Number of elements to be pushed"
n=gets.to_i
n.times do
puts "Enter element"
el=gets.to_s
array.push el
end
puts array
elsif choice==2
puts array.pop
elsif choice==3
puts array.length
else
puts "invalid"
end
end
当我在
if choice==1
中打印我的数组时,我得到了所有输出在不同行上的结果,
例如:hello
i
am
beginner
to
ruby
有没有办法将输出放在单行中? 例如:
hello i am beginner to ruby
编辑:我甚至尝试过使用puts array.join(' ')
,但也不起作用。
puts array.join(' ')
- tckmnchomp
函数处理输入。 - Dave Newton