Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > %s[ I think %s[] should build an array of symbols instead of asingle symbol ]

Reply
Thread Tools

%s[ I think %s[] should build an array of symbols instead of asingle symbol ]

 
 
Joey Zhou
Guest
Posts: n/a
 
      03-05-2011
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?

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
 
botp
Guest
Posts: n/a
 
      03-06-2011
On Sat, Mar 5, 2011 at 6:33 PM, Joey Zhou <(E-Mail Removed)> wrote:
> Maybe %s[] should be changed, like %w[], not %q[]. Anyone think so?


and so everyone elses programs

check if %S is unused...

 
Reply With Quote
 
 
 
 
Joey Zhou
Guest
Posts: n/a
 
      03-06-2011
botp wrote in post #985687:
> and so everyone elses programs
> check if %S is unused...


If %s[] builds array of symbols, it will save numerous
"colon-comma-colon-comma..."

--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-08-2011
On Sat, Mar 5, 2011 at 11:33 AM, Joey Zhou <(E-Mail Removed)> wrote:
> 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}:


Or even

irb(main):004:0> %w[ I think this should build an array of symbols
].map &: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.


I am not sure what you mean by that. The code with #map uses #to_sym
in a loop so both are involved. On the other hand %s... does not use
to_sym at all I believe.

Also, expensiveness only matters if you execute the code really
frequently. An Array of Symbols sounds like something one would
create once and store it in a class constant for example - so no need
to worry about efficiency here.

> And I think the demand for an array of symbols is more frequent
> than a single sentence-like symbol.


But the demand for Symbols containing whitespace and other special
characters might be even higher than the demand for an Array of
Symbols.

> Maybe %s[] should be changed, like %w[], not %q[]. Anyone think so?


Maybe, I don't.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A critical update is available to remove unacceptable symbols fromthe Bookshelf Symbol 7 font SchoolTech NZ Computing 5 02-03-2006 08:00 AM
Need help: Is Quick-Union-Find the right solution to this problem (Now I don't think so and I think that topological sorting should be the way to go...?) ? aredo3604gif@yahoo.com C Programming 1 04-13-2005 12:48 AM
Need help: Is Quick-Union-Find the right solution to this problem (Now I don't think so and I think that topological sorting should be the way to go...?) ? aredo3604gif@yahoo.com C Programming 0 04-12-2005 05:06 PM
Correct way to handle independent interpreters when embedding in asingle-threaded C++ app Craig Ringer Python 1 11-18-2004 11:35 PM
SWsoft Acronis Disk Director Suite 9.0 Build 508, Acronis OS Selector 8.0 Build 917, Acronis Partition Expert 2003 Build 292, Acronis Power Utilities 2004 Build 502, F-SECURE.ANTI vIRUS.PROXY v1.10.17.WINALL, F-SECURE.ANTI vIRUS v5.50.10260 for CITRI vvcd Computer Support 0 09-25-2004 01:38 AM



Advertisments