[英]Is there a more elegant way to perform a multiple include? selection in ruby?
I came across this issue in a project last night, while writing a helper to pick an icon depending on a file extension, and was wondering if there was a better (more "ruby") way of handling it? 我昨晚在一个项目中遇到了这个问题,当时我写了一个帮助程序来根据文件扩展名选择图标,并且想知道是否有更好的(更“红宝石”)处理方式?
The code currently goes something like: 该代码当前类似于:
def choose_icon(myfile)
icon = "default-icon"
if ["*.doc","*.docx",".txt","*.dot"].include? File.extname(myfile)
icon = "doc-icon"
end
if ["*.xls","*.xlsx","*.xlt"].include? File.extname(myfile)
icon = "sheet-icon"
end
if ["*.mp3","*.aac","*.aiff","*.wav"].include? File.extname(myfile)
icon = "audio-icon"
end
if ["*.mov","*.m4a","*.wmv"].include? File.extname(myfile)
icon = "movie-icon"
end
icon # return the chosen icon
end
This somehow feels a little clumsy and inelegant to me, and I was struggling to find a better way to do it in Ruby. 这在某种程度上让我感到笨拙和笨拙,而我一直在努力寻找在Ruby中实现此目标的更好方法。
(Note: The above example is REALLY simplified and the actual code is far longer and looks far more untidy.) (注意:上面的示例已真正简化,实际代码更长,看起来更加混乱。)
It would be really cool if the ' case ' construct would work like this: 如果' case '构造像这样工作,那将真的很酷:
def choose_icon(myfile)
case File.extname(myfile)
when ["*.doc","*.docx",".txt","*.dot"].include?
"doc-icon"
when ["*.xls","*.xlsx","*.xlt"].include?
"sheet-icon"
when ["*.mp3","*.aac","*.aiff","*.wav"].include?
"audio-icon"
when ["*.mov","*.m4a","*.wmv"].include?
"movie-icon"
else
"default-icon"
end
end
But of course, that doesn't work. 但是,当然不行。 It is so much easier to read though, so I was wondering if I had missed some other method of doing a multiple comparison against a collection of options that would restore some elegance and readability to my code? 但是,它更容易阅读,所以我想知道是否错过了其他一些与选项集合进行多重比较的方法,这些选项将使我的代码恢复一些优雅和可读性吗?
You almost got it right. 你几乎是对的。 Just leave off the square brackets and the include?
只需删除方括号和include?
and it will work. 它会工作。 I think the asterisks should not be necessary either since File.extname
returns the extension with just a dot. 我认为星号也不必要,因为File.extname
返回的扩展名只是一个点。
def choose_icon(myfile)
case File.extname(myfile)
when '.doc', '.docx', '.txt', '.dot'
'doc-icon'
when '.xls', '.xlsx', '.xlt'
'sheet-icon'
when '.mp3', '.aac', '.aiff', '.wav'
'audio-icon'
when '.mov', '.m4a', '.wmv'
'movie-icon'
else
'default-icon'
end
end
You could use a hash: 您可以使用哈希:
h = [*(%w| .doc .docx .txt .dot |).product(["doc-icon"]),
*(%w| .xls .xlsx .xlt |).product(["sheet-icon"]),
*(%w| .aac .aiff .wav |).product(["audio-icon"]),
*(%w| .mov .m4a .wmv |).product(["movie-icon"])].to_h
#=> {".doc"=>"default-icon", ".docx"=>"default-icon",
# ".txt"=>"default-icon", ".dot"=>"default-icon",
# ".xls"=>"sheet-icon" , ".xlsx"=>"sheet-icon",
# ".xlt"=>"sheet-icon" , ".aac"=>"audio-icon",
# ".aiff"=>"audio-icon" , ".wav"=>"audio-icon",
# ".mov"=>"movie-icon" , ".m4a"=>"movie-icon",
# ".wmv"=>"movie-icon"}
h.default = "default-icon"
h[File.extname("myfile.wav")]
#=> "audio-icon"
h[File.extname("./mydir/myfile.abc")]
#=> "default-icon"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.