Arrays 在数组文字“%w”中将单词分组在一起`
有没有办法在Arrays 在数组文字“%w”中将单词分组在一起`,arrays,ruby,literals,Arrays,Ruby,Literals,有没有办法在%w(foo-bar)符号中将多个单词组合在一起,以获得如下结果数组 %w(foo bar foo or bar foo and bar) # => ["foo", "bar", "foo or bar", "foo and bar"] 是的,有办法。您可以使用\删除空白: %w(foo bar foo\ or\ bar foo\ and\ bar) #=> ["foo", "bar", "foo or bar", "foo and bar"] 但我不确定这是否能提高
%w(foo-bar)
符号中将多个单词组合在一起,以获得如下结果数组
%w(foo bar foo or bar foo and bar) # => ["foo", "bar", "foo or bar", "foo and bar"]
是的,有办法。您可以使用
\
删除空白:
%w(foo bar foo\ or\ bar foo\ and\ bar)
#=> ["foo", "bar", "foo or bar", "foo and bar"]
但我不确定这是否能提高可读性…是的,有一种方法。您可以使用
\
删除空白:
%w(foo bar foo\ or\ bar foo\ and\ bar)
#=> ["foo", "bar", "foo or bar", "foo and bar"]
但我不确定这是否会提高可读性…可以使用
\
作为空格字符:
%w(foo bar foo\ or\ bar foo\ and\ bar) => ["foo", "bar", "foo or bar", "foo and bar"]
可以使用
\
作为空格字符:
%w(foo bar foo\ or\ bar foo\ and\ bar) => ["foo", "bar", "foo or bar", "foo and bar"]
不使用
%w
符号,但使用类似的精神,使用|
而不是空格作为分隔符:
"foo|bar|foo or bar|foo and bar".split("|")
不使用
%w
符号,但使用类似的精神,使用|
而不是空格作为分隔符:
"foo|bar|foo or bar|foo and bar".split("|")
你不认为简单地写
[“foo”,“bar”,“foo或bar”,“foo和bar”]
比尝试找出任何技巧(如果有的话)要简单得多吗?为什么要坚持%w
?那(foo或bar或bat)
或(for或bar和bat)
呢?你不认为简单地写[“foo”、“bar”、“foo或bar”、“foo和bar”]
比试图想出任何把戏(如果有的话)要简单得多吗?为什么要坚持%w
?那(foo或bar或bat)
或(for或bar和bat)
呢?