gpt4 book ai didi

Ruby 在数组中查找下一个

转载 作者:数据小太阳 更新时间:2023-10-29 06:47:22 24 4
gpt4 key购买 nike

有没有办法在 Ruby 数组中找到下一个项目?

代码:

# Find ALL languages
if !debug
lang = Language.all
else
lang = Language.where("id = ? OR id = ?", 22, 32)
end

# Get all elements
elements = Element.where("human_readable IS NOT NULL")

lang.each do |l|
code = l.code.downcase
if File.exists?(file_path + code + ".yml")
File.delete(file_path + code + ".yml")
end

t1 = Time.now

info = {}
elements.each do |el|
unless l.id == 1
et = el.element_translations.where("language_id = ? AND complete = ?", l.id, true)
else
et = el.element_translations.where("language_id = ?", 1)
end
et.each do |tran|
info[code] ||= {}
info[code][el.human_readable] = tran.content.gsub("\n", "").force_encoding("UTF-8").encode!
end
end
File.open(file_path + code + ".yml", "w", :encoding => "UTF-8") do |f|
if f.write(info.to_yaml)
t2 = Time.now

puts code + ".yml File written"
puts "It took " + time_diff_milli(t1, t2).to_s + " seconds to complete"
# This is where I want to display the next item in the lang array
puts lang.shift(1).inspect
puts "*"*50
end
end
end

最佳答案

Array 包括 Enumerable,因此您可以使用 each_with_index :

elements.each_with_index {|element, index|
next_element = elements[index+1]
do_something unless next_element.nil?
...

}

关于Ruby 在数组中查找下一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4596517/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com