小编wha*_*t11的帖子

将2d数组转换为1d(Ruby)

下面的代码目前将#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)

ruby arrays

1
推荐指数
1
解决办法
2209
查看次数

长循环与短循环Ruby

使用下面的代码作为例子,有人可以给我一个循环方法的单行代码的例子,并向我解释差异?我知道循环有一个短版本和长版本,我认为这对社区搜索(以及我自己的理解)有帮助.

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)

ruby loops

0
推荐指数
2
解决办法
164
查看次数

标签 统计

ruby ×2

arrays ×1

loops ×1