告诉 ruby 中 .each 循环的结束
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4000713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Tell the end of a .each loop in ruby
提问by Splashlin
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 是多个用户的散列。什么是最简单的条件逻辑来查看您是否在用户哈希中的最后一个用户上并且只想为最后一个用户执行特定代码,例如
users.each do |u|
#code for everyone
#conditional code for last user
#code for the last user
end
end
回答by Raphomet
users.each_with_index do |u, index|
# some code
if index == users.size - 1
# code for the last user
end
end
回答by meagar
If it's an either/or situation, where you're applying some code to all butthe last user and then some unique code to onlythe last user, one of the other solutions might be more appropriate.
如果这是一个非此即彼/或情况,在那里你将一些代码给所有,但最后一个用户,然后一些独特的代码,只有最后一个用户的另一种解决方案可能更为合适。
However, you seem to be running the same code for all users, and some additionalcode for the last user. If that's the case, this seems more correct, and more clearly states your intent:
但是,您似乎为所有用户运行相同的代码,并为最后一个用户运行一些额外的代码。如果是这样,这似乎更正确,并且更清楚地说明了您的意图:
users.each do |u|
#code for everyone
end
users.last.do_stuff() # code for last user
回答by Alter Lagos
I think a best approach is:
我认为最好的方法是:
users.each do |u|
#code for everyone
if u.equal?(users.last)
#code for the last user
end
end
回答by Teja Kantamneni
Did you tried each_with_index?
你试过each_with_index吗?
users.each_with_index do |u, i|
if users.size-1 == i
#code for last items
end
end
回答by DigitalRoss
h = { :a => :aa, :b => :bb }
h.each_with_index do |(k,v), i|
puts ' Put last element logic here' if i == h.size - 1
end
回答by xlembouras
Sometimes I find it better to separate the logic to two parts, one for all users and one for the last one. So I would do something like this:
有时我发现将逻辑分成两部分更好,一部分用于所有用户,另一部分用于最后一个。所以我会做这样的事情:
users[0...-1].each do |user|
method_for_all_users user
end
method_for_all_users users.last
method_for_last_user users.last
回答by coderuby
You can use @meager's approach also for an either/or situation, where you're applying some code to all but the last user and then some unique code to only the last user.
您也可以将@meager 的方法用于非此即彼的情况,在这种情况下,您将一些代码应用于除最后一个用户之外的所有用户,然后将一些唯一代码应用于仅最后一个用户。
users[0..-2].each do |u|
#code for everyone except the last one, if the array size is 1 it gets never executed
end
users.last.do_stuff() # code for last user
This way you don't need a conditional!
这样你就不需要条件了!
回答by ricardokrieg
Another solution is to rescue from StopIteration:
另一种解决方案是从 StopIteration 中拯救:
user_list = users.each
begin
while true do
user = user_list.next
user.do_something
end
rescue StopIteration
user.do_something
end
回答by Sathianarayanan Sundaram
There are no last method for hash for some versions of ruby
某些版本的 ruby 没有最后的哈希方法
h = { :a => :aa, :b => :bb }
last_key = h.keys.last
h.each do |k,v|
puts "Put last key #{k} and last value #{v}" if last_key == k
end

