下面的代码目前将#startyear的副本推送到#new.我需要把它转换成1个单个数组,任何想法?论坛没有多少
startyear = [["a", "b", "z"], ["c", "d"], ["e", "f"], ["g", "h", "i", "j"]]
new = []
startyear.each do |n| #.transpose here?
puts "looping #{n}"
new.push(n)
#n.join is needed somewhere
puts "#{startyear.length} is the length of startyear"
break if startyear.length == startyear.length[4]
end
puts "your new array is : #{new}"
Run Code Online (Sandbox Code Playgroud) 使用下面的代码作为例子,有人可以给我一个循环方法的单行代码的例子,并向我解释差异?我知道循环有一个短版本和长版本,我认为这对社区搜索(以及我自己的理解)有帮助.
def first_square_numbers(number_of_squares)
squares = []
idx = 0
while idx < number_of_squares
squares.push(idx * idx)
idx = idx + 1
end
return squares
end
Run Code Online (Sandbox Code Playgroud)