告诉红宝石.each循环的结尾 [英] Tell the end of a .each loop in ruby

查看:47
本文介绍了告诉红宝石.each循环的结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个循环,例如

If i have a loop such as

users.each do |u|
  #some code
end

用户是多个用户的哈希.查看您是否位于用户哈希中的最后一个用户并且只想为该最后一个用户执行特定代码的最简单的条件逻辑是什么,例如

Where users is a hash of multiple users. What's the easiest conditional logic to see if you are on the last user in the users hash and only want to execute specific code for that last user so something like

users.each do |u|
  #code for everyone
  #conditional code for last user
    #code for the last user
  end
end

推荐答案

users.each_with_index do |u, index|
  # some code
  if index == users.size - 1
    # code for the last user
  end
end

这篇关于告诉红宝石.each循环的结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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