审查ruby"if"声明

1 ruby string if-statement

你有什么建议让这个if条件更短(更优雅)吗?

if (@path.start_with? "scp" || @path.start_with? "http")
  @source = "url"
else
  @source = "local"
end
Run Code Online (Sandbox Code Playgroud)

如果我有更多的前缀要检查(让我们说ftp1,ftp2和ftp3)怎么办?

roh*_*t89 5

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

@source = @path.start_with?("scp", "http") ? "url" : "local"
Run Code Online (Sandbox Code Playgroud)