这是我唯一的选择吗?
%w(123 456).map { |i| i.to_i }
Run Code Online (Sandbox Code Playgroud)
我认为%i(123 456)会包括在内,但我想不会.
这样的实用方法可用于处理引用和转义问题,这些问题都不存在于数字列表中.
例如,处理使用两种报价样式的情况:
'Johnny\'s dad\'s friend said, "I really hate backslashes!"'
%Q{Johnny's dad's friend said, "I really hate backslashes!"}
Run Code Online (Sandbox Code Playgroud)
对于字符串列表:
[ 'this', 'that', 'and', 'the', 'other', 'thing' ]
%w[ this that and the other thing ]
Run Code Online (Sandbox Code Playgroud)
对于经常出现斜杠的正则表达式:
/https?:\/\/([\w\-]+).com\//
%r{https?://([\w\-]+).com/}
Run Code Online (Sandbox Code Playgroud)
但是,在定义数字列表时,不需要特定的转义.
| 归档时间: |
|
| 查看次数: |
1841 次 |
| 最近记录: |