gpt4 book ai didi

ruby - 审查 ruby​​ "if"声明

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

对于以某种方式使这个 if 条件更短(更优雅),你有什么建议吗?

if (@path.start_with? "scp" || @path.start_with? "http")
@source = "url"
else
@source = "local"
end

如果我要检查的前缀很少(比如 ftp1、ftp2 和 ftp3)怎么办?

最佳答案

start_with? 可以接受多个字符串作为参数

@source = @path.start_with?("scp", "http") ? "url" : "local"

关于ruby - 审查 ruby​​ "if"声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33653529/

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