在每个循环中找出当前索引(Ruby) [英] Finding out current index in EACH loop (Ruby)

查看:18
本文介绍了在每个循环中找出当前索引(Ruby)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
Ruby 中的自动计数器?

我想在每个循环中找出当前索引.我该怎么做?

I want to find out the current index while i am in the each loop. how do i do so?

X=[1,2,3]

X.each do |p|
 puts "current index..."
end 

推荐答案

X.each_with_index do |item, index|
  puts "current_index: #{index}"
end

这篇关于在每个循环中找出当前索引(Ruby)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆