[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: "" vs ''

James Gray

7/20/2006 6:59:00 PM

On Jul 20, 2006, at 1:48 PM, Ben Johnson wrote:

> I know this is a very simply question, but I've seen many different
> responses.
>
> Isn't it better to use 'some string' instead of "some string".
> Because the "" goes through a lot more working interpreting any
> variable that might be in the string, etc. Where as '' is just
> literal.

'...' goes through processing too, just less. \' and \\ are special
inside '...'.

Is there a time difference though? Let's ask Ruby:

#!/usr/bin/env ruby -w

require "benchmark"

TESTS = 10_000_000.freeze
Benchmark.bmbm(10) do |results|
results.report("single:") { TESTS.times { 'My String' } }
results.report("double:") { TESTS.times { "My String" } }
end
# >> Rehearsal ---------------------------------------------
# >> single: 2.840000 0.010000 2.850000 ( 2.861201)
# >> double: 2.870000 0.000000 2.870000 ( 2.885730)
# >> ------------------------------------ total: 5.720000sec
# >>
# >> user system total real
# >> single: 2.870000 0.010000 2.880000 ( 2.891390)
# >> double: 2.850000 0.000000 2.850000 ( 2.869507)

__END__

Doesn't look like it. Guess Ruby is pretty smart about handling this.

End facts. Begin opinions...

I use to use '...' all the time thinking it was a good programming
habit. Unfortunately, it just meant I had to switch to "..." every
time I belatedly realized I would need some interpolation. So, as
I've gotten lazier, I've pretty much switched to using "..." all the
time.

James Edward Gray II