[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: "" vs ''

dave

9/25/2006 4:53:00 PM



Use " by default:

- It makes the code more aestethic.
- It helps when you have to put some inside like #{} '.




--
Upper reality >oftware.
Dave - Skp Core.


--
Email.it, the professional e-mail, gratis per te: http://www....

Sponsor:
Le speciali Offerte di Benvenuto di Cassine di Pietra:
* scopra il gusto ed i vantaggi delle tradizioni contadine
*
Clicca qui: http://adv.email.it/cgi-bin/foclick.cgi?mid=3924&...

12 Answers

Rich Morin

9/25/2006 5:04:00 PM

0

Use '' by default:

- It's less work for the interpreter.

- It alerts the reader to impending "#{magic}".

-r
--
http://www.cf... Rich Morin
http://www.cf.../resume rdm@cfcl.com
http://www.cf.../weblog +1 650-873-7841

Technical editing and writing, programming, and web development

James Gray

9/25/2006 6:11:00 PM

0

On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:

> Use '' by default:
>
> - It's less work for the interpreter.

What makes you say this?

#!/usr/bin/env ruby -w

require "benchmark"

TESTS = 1_000_000
Benchmark.bmbm(10) do |results|
results.report("double:") { TESTS.times { "James" } }
results.report("single:") { TESTS.times { 'James' } }
end
# >> Rehearsal ---------------------------------------------
# >> double: 0.270000 0.000000 0.270000 ( 0.267826)
# >> single: 0.260000 0.000000 0.260000 ( 0.266784)
# >> ------------------------------------ total: 0.530000sec
# >>
# >> user system total real
# >> double: 0.270000 0.000000 0.270000 ( 0.268957)
# >> single: 0.290000 0.000000 0.290000 ( 0.286691)

James Edward Gray II

Rich Morin

9/25/2006 7:10:00 PM

0

At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:
> On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
>
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?

If the interpreter sees a single-quoted string, it knows
that it doesn't need to scan the string for things (such
as escape sequences) to expand. I agree that this effect
will be lost in the noise, in most cases, but why make the
interpreter do extra work?

The real point, in any case, is one of making things clear
to the reader. If a quoted string uses double quotes, the
reader has to look it over to determine whether any magic
is being performed. This wastes some of the reader's time
(and no, I don't have a benchmark for this. :-).

-r
--
http://www.cf... Rich Morin
http://www.cf.../resume rdm@cfcl.com
http://www.cf.../weblog +1 650-873-7841

Technical editing and writing, programming, and web development

Joel VanderWerf

9/25/2006 7:20:00 PM

0

Rich Morin wrote:
...
> The real point, in any case, is one of making things clear
> to the reader. If a quoted string uses double quotes, the
> reader has to look it over to determine whether any magic
> is being performed. This wastes some of the reader's time
> (and no, I don't have a benchmark for this. :-).

True, but syntax hiliting can help spot the #{...}.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Christian Neukirchen

9/25/2006 8:00:00 PM

0

James Edward Gray II <james@grayproductions.net> writes:

> On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
>
>> Use '' by default:
>>
>> - It's less work for the interpreter.
>
> What makes you say this?
>
> #!/usr/bin/env ruby -w
>
> require "benchmark"
>
> TESTS = 1_000_000
> Benchmark.bmbm(10) do |results|
> results.report("double:") { TESTS.times { "James" } }
> results.report("single:") { TESTS.times { 'James' } }
> end

You really should compare the parse-trees of these before even trying
to benchmark it that way...

> James Edward Gray II
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Rick DeNatale

9/25/2006 9:29:00 PM

0

On 9/25/06, Christian Neukirchen <chneukirchen@gmail.com> wrote:
> James Edward Gray II <james@grayproductions.net> writes:
>
> > On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
> >
> >> Use '' by default:
> >>
> >> - It's less work for the interpreter.
> >
> > What makes you say this?
> >
> > #!/usr/bin/env ruby -w
> >
> > require "benchmark"
> >
> > TESTS = 1_000_000
> > Benchmark.bmbm(10) do |results|
> > results.report("double:") { TESTS.times { "James" } }
> > results.report("single:") { TESTS.times { 'James' } }
> > end
>
> You really should compare the parse-trees of these before even trying
> to benchmark it that way...

Du hast ist recht!

Any differences due to parsing are going to happen when the source
code is parsed, which will happen before the benchmark is run.

I believe that this gives a better handle on the relative parsing
overhead, there doesn't seem to be much difference.

rick@frodo:/public/rubyscripts$ cat slbench.rb
require 'benchmark'

TESTS = 1_000_000
Benchmark.bmbm(10) do |results|
results.report("double:") {TESTS.times{eval("\"James\"")}}
results.report("single:") {TESTS.times{eval('\'James\'')}}
end
rick@frodo:/public/rubyscripts$ ruby slbench.rb
Rehearsal ---------------------------------------------
double: 31.990000 1.030000 33.020000 ( 55.318747)
single: 32.080000 1.120000 33.200000 ( 55.743170)
----------------------------------- total: 66.220000sec

user system total real
double: 32.160000 1.040000 33.200000 ( 56.097062)
single: 32.150000 1.090000 33.240000 ( 53.597692)
rick@frodo:/public/rubyscripts$



--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

IPMS/USA Region 12 Coordinator
http://ipmsr12.denh...

Visit the Project Mercury Wiki Site
http://www.mercuryspace...

Logan Capaldo

9/25/2006 11:41:00 PM

0

On Tue, Sep 26, 2006 at 05:00:17AM +0900, Christian Neukirchen wrote:
> James Edward Gray II <james@grayproductions.net> writes:
>
> > On Sep 25, 2006, at 12:03 PM, Rich Morin wrote:
> >
> >> Use '' by default:
> >>
> >> - It's less work for the interpreter.
> >
> > What makes you say this?
> >
> > #!/usr/bin/env ruby -w
> >
> > require "benchmark"
> >
> > TESTS = 1_000_000
> > Benchmark.bmbm(10) do |results|
> > results.report("double:") { TESTS.times { "James" } }
> > results.report("single:") { TESTS.times { 'James' } }
> > end
>
> You really should compare the parse-trees of these before even trying
> to benchmark it that way...
>
In the non-interpolation case the parse-trees are identical:

% cat strings.rb
f('James', "James")

% cat strings.rb | parse_tree_show -f
[[:fcall, :f, [:array, [:str, "James"], [:str, "James"]]]]
> > James Edward Gray II
> --
> Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...

Nobuyoshi Nakada

9/26/2006 5:07:00 AM

0

Hi,

At Tue, 26 Sep 2006 04:09:54 +0900,
Rich Morin wrote in [ruby-talk:216346]:
> At 3:10 AM +0900 9/26/06, James Edward Gray II wrote:
> >> Use '' by default:
> >>
> >> - It's less work for the interpreter.
> >
> > What makes you say this?
>
> If the interpreter sees a single-quoted string, it knows
> that it doesn't need to scan the string for things (such
> as escape sequences) to expand. I agree that this effect
> will be lost in the noise, in most cases, but why make the
> interpreter do extra work?

It knows that it does need to scan the string always for the
terminator, and even escaped quotes.

--
Nobu Nakada

Paul Battley

9/26/2006 8:57:00 AM

0

On 25/09/06, dave <dave.m@email.it> wrote:
>
>
> Use " by default:
>
> - It makes the code more aestethic.
> - It helps when you have to put some inside like #{} '.

I often end up typing ' simply because it's an unmodified key on my
keyboard, unlike " which necessitates pressing shift as well.

Here's a little trick for when you have a '-quoted string that you
want to add interpolation to: just add %:

before:
'foo #{3}' # => "foo #{3}"

after:
%'foo #{3}' # => "foo 3"

I'm not saying you should do it all the time, but it's occasionally
nice to save a few keystrokes when playing around with some code.

Paul.

Christian Neukirchen

9/26/2006 4:31:00 PM

0

"Rick DeNatale" <rick.denatale@gmail.com> writes:

>> You really should compare the parse-trees of these before even trying
>> to benchmark it that way...
>
> Du hast ist recht!

Drop the "ist".

> Any differences due to parsing are going to happen when the source
> code is parsed, which will happen before the benchmark is run.
>
> I believe that this gives a better handle on the relative parsing
> overhead, there doesn't seem to be much difference.
>
> rick@frodo:/public/rubyscripts$ cat slbench.rb
> require 'benchmark'
>
> TESTS = 1_000_000
> Benchmark.bmbm(10) do |results|
> results.report("double:") {TESTS.times{eval("\"James\"")}}
> results.report("single:") {TESTS.times{eval('\'James\'')}}
> end
> rick@frodo:/public/rubyscripts$ ruby slbench.rb
> Rehearsal ---------------------------------------------
> double: 31.990000 1.030000 33.020000 ( 55.318747)
> single: 32.080000 1.120000 33.200000 ( 55.743170)
> ----------------------------------- total: 66.220000sec
>
> user system total real
> double: 32.160000 1.040000 33.200000 ( 56.097062)
> single: 32.150000 1.090000 33.240000 ( 53.597692)
> rick@frodo:/public/rubyscripts$

And now the difference gets lost in noise because the strings aren't
long enough. :-)

In the end, it doesn't matter.

> Rick DeNatale
--
Christian Neukirchen <chneukirchen@gmail.com> http://chneuk...