J
Joey Zhou
I find %s[] builds a single symbol.
p %s[ I think this should build an array of symbols ]
=> :" I think this should build an array of symbols "
It even costs one more keystroke than :"", so it has no effect to make
fingers happy. If the purpose of %s[] is to avoid quotation mark
confusion, I think %q[].to_sym is enough, and more flexible: %Q[].to_sym
works too (well, there's no %S[]).
what = "an array of symbols"
p %Q[ I think this should build #{what} ].to_sym
p %q[ I think this should build an array of symbols ].to_sym
If I want an array of symbols, I can use %w[].map {|x| x.to_sym}:
p %w[ I think this should build an array of symbols ].map {|x| x.to_sym}
=> [:I, :think, :this, :should, :build, :an, :array,
f, :symbols]
But I think .map {|x| x.to_sym} is more expensive than .to_sym, because
map changes so many elements while .to_sym only changes a single
object. And I think the demand for an array of symbols is more frequent
than a single sentence-like symbol.
Maybe %s[] should be changed, like %w[], not %q[]. Anyone think so?
p %s[ I think this should build an array of symbols ]
=> :" I think this should build an array of symbols "
It even costs one more keystroke than :"", so it has no effect to make
fingers happy. If the purpose of %s[] is to avoid quotation mark
confusion, I think %q[].to_sym is enough, and more flexible: %Q[].to_sym
works too (well, there's no %S[]).
what = "an array of symbols"
p %Q[ I think this should build #{what} ].to_sym
p %q[ I think this should build an array of symbols ].to_sym
If I want an array of symbols, I can use %w[].map {|x| x.to_sym}:
p %w[ I think this should build an array of symbols ].map {|x| x.to_sym}
=> [:I, :think, :this, :should, :build, :an, :array,
But I think .map {|x| x.to_sym} is more expensive than .to_sym, because
map changes so many elements while .to_sym only changes a single
object. And I think the demand for an array of symbols is more frequent
than a single sentence-like symbol.
Maybe %s[] should be changed, like %w[], not %q[]. Anyone think so?