gpt4 book ai didi

ruby-on-rails - 是否可以在 ruby​​ 循环中增加符号键的名称?

转载 作者:太空宇宙 更新时间:2023-11-03 17:05:14 24 4
gpt4 key购买 nike

我正在尝试打印 n 并将其标记在符号键的末尾,每次 n 递增;这可能吗?

这是一段代码...

# User info
client = Instagram.client(:access_token => session[:access_token])
@user = Hash.new
@user[:info] = client.user

# User media
@user[:recent_media] = Hash.new
@user[:recent_media][:p1] = client.user_recent_media
# user_recent_media only ever retrieves in 20 out of x image links; x being total profile images

(1..9).each do |n|
page_max_id = @user[:recent_media][:p<n>].pagination.next_max_id
return if page_max_id.nil?
@user[:recent_media][:p<n+1>] = client.user_recent_media(max_id: page_max_id)
end

最佳答案

您要避免在此处使用符号作为键,因为您是动态生成它们的。符号永远不会被垃圾收集,因此会导致内存泄漏。如果可以更改为字符串,则可以进行字符串插值,例如“p#{n + 1}”。否则,在最后用 .to_sym 做同样的事情会起作用,但要注意。

关于ruby-on-rails - 是否可以在 ruby​​ 循环中增加符号键的名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280695/

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