gpt4 book ai didi

ruby - 迭代一个常数是否是最佳实践?

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

在我的 Ruby 脚本中,我在方法之外声明了一个常量:

SYNDICATIONS = %w(Advanced Syndication, Boxee feed, Player MRSS, iPad MRSS, iPhone MRSS, YouTube)

并以如下方法迭代它:

def some_method
SYNDICATIONS.each do |syndication|
puts syndication
end
end

迭代一个常量是不是一个好主意?

最佳答案

迭代没有错。但是,常量的定义存在错误。 %w 运算符并不像您认为的那样工作。它在空格而不是逗号上拆分标记。如果您希望空格不是定界符,请将其转义。比较这三个例子,看看哪个最清楚。

a1 = %w(Advanced Syndication, Boxee feed, Player MRSS, iPad MRSS, iPhone MRSS, YouTube)
a1 # => ["Advanced", "Syndication,", "Boxee", "feed,", "Player", "MRSS,", "iPad", "MRSS,", "iPhone", "MRSS,", "YouTube"]

a2 = %w(Advanced\ Syndication Boxee\ feed Player\ MRSS iPad\ MRSS iPhone\ MRSS YouTube)
a2 # => ["Advanced Syndication", "Boxee feed", "Player MRSS", "iPad MRSS", "iPhone MRSS", "YouTube"]

a3 = ["Advanced Syndication", "Boxee feed", "Player MRSS", "iPad MRSS", "iPhone MRSS", "YouTube"]
a3 # => ["Advanced Syndication", "Boxee feed", "Player MRSS", "iPad MRSS", "iPhone MRSS", "YouTube"]

关于ruby - 迭代一个常数是否是最佳实践?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16476628/

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