Ruby %I{ .. } returns a list of symbols with interpolation

hooopo · 2012年07月25日 · 最后由 Saito 回复于 2012年07月25日 · 2592 次阅读

https://github.com/ruby/ruby/commit/91bd6e711db3418baa287e936d4b0fac99927711

终于可以%i{a b c d}这样创建符号列表了。。

是不是出来的结果便成?:

[:a, :b, :c, :d]

#1 楼 @huacnlee 是的

ruby -v -e "p %i{a b c}"
ruby 2.0.0dev (2012-07-25 trunk 36531) [i686-linux]
[:a, :b, :c]

irb> %w(a b c d)
["a","b","c","d"]
irb> %i(a b c d)
[:a, :b, :c, :d]
irb> %(a b c d)
"a b c d"
irb> %r(a b c d)
/a b c d/
irb> 没有有变成数组的?
[4,2,1,3]

#3 楼 @huacnlee 这个真没有。。一般都用 range 吧 还有就是 ruby 数组写法本身就简单,字符串和符号需要敲引号和冒号,所以才有了各种内插..

昨天看 josevalim 问 terderlove 为啥是 %i %I 不是 %s

terderlove 解释说: %S 被用了。String.intern 是 String.to_sym 的 alias..

josevalim 说:make sense. <===3

需要 登录 后方可回复, 如果你还没有账号请 注册新账号