Double and single quote usage in AWDWR

Hello everyone!

I’ve noticed, that the book uses double quotes almost everywhere. It is
like

<%= javascript_include_tag “prototype” %>

I’ve checked the “programming ruby, 2ed” book and found that double
quotes are most suited when escape sequences and substitutions are
required. Isn’t it more semantically correct to write this code like

<%= javascript_include_tag ‘prototype’ %>

for we don’t need to substitute anything in this string. Or is it a
matter of style only in this case? If I stick to second way, will
templates be processed faster?

olegf

Oleg F. wrote:

<%= javascript_include_tag ‘prototype’ %>

for we don’t need to substitute anything in this string. Or is it a
matter of style only in this case? If I stick to second way, will
templates be processed faster?
It’s certainly more correct, but I doubt the speed difference would be
noticeable. The only way to be sure would be to measure it, though…

for we don’t need to substitute anything in this string. Or is it a
matter of style only in this case? If I stick to second way, will
templates be processed faster?

You should probably use whichever form you feel comfortable with. I
personally choose randomly, but tend to favor double quotes. This is,
after all, Ruby, where happiness trumps consistency.

Every time I’ve measured speed, I’m found the two forms to be about
the same.

Dave

Am Dienstag, den 21.03.2006, 11:57 +0000 schrieb Alex Y.:

<%= javascript_include_tag ‘prototype’ %>

for we don’t need to substitute anything in this string. Or is it a
matter of style only in this case? If I stick to second way, will
templates be processed faster?
It’s certainly more correct, but I doubt the speed difference would be
noticeable. The only way to be sure would be to measure it, though…

I always thought there is a difference, but this test shows me there
isn’t. Or is something wrong in my test?

LINES = 50000
TESTS = 10

singles = []
doubles = []

def rand_string
Array.new(10).collect { (65 + rand(25)).chr }.join
end

File.open(‘single_quotes.rb’, ‘w’) do |file|
file.puts ‘now = Time.now.to_f’
LINES.times { file.puts “foo = ‘#{rand_string}’” }
file.puts ‘puts Time.now.to_f - now’
end

File.open(‘double_quotes.rb’, ‘w’) do |file|
file.puts ‘now = Time.now.to_f’
LINES.times { file.puts “foo = "#{rand_string}"” }
file.puts ‘puts Time.now.to_f - now’
end

TESTS.times do
singles << ruby single_quotes.rb.to_f
doubles << ruby double_quotes.rb.to_f
end

puts 'single: ’ + (singles.inject(0.0) { |sum, value| sum += value;
sum } / singles.size).to_s
puts 'double: ’ + (doubles.inject(0.0) { |sum, value| sum += value;
sum } / doubles.size).to_s


Norman T.

http://blog.inlet-media.de

Am Dienstag, den 21.03.2006, 13:24 +0000 schrieb Alex Y.:

Norman T. wrote:

I always thought there is a difference, but this test shows me there
isn’t. Or is something wrong in my test?
Well, I just ran it and got a higher number for single-quotes than for
double, so I’m guessing there’s something confounding it somewhat…

This is the same for me. But, it also the same, if i write a complete
different test. Sometimes single quotes are faster, sometimes doubles.


Norman T.

http://blog.inlet-media.de

Norman T. wrote:

I always thought there is a difference, but this test shows me there
isn’t. Or is something wrong in my test?
Well, I just ran it and got a higher number for single-quotes than for
double, so I’m guessing there’s something confounding it somewhat…


Alex

Norman T. wrote:

I always thought there is a difference, but this test shows me there
isn’t. Or is something wrong in my test?

File.open(‘single_quotes.rb’, ‘w’) do |file|
file.puts ‘now = Time.now.to_f’
LINES.times { file.puts “foo = ‘#{rand_string}’” }
file.puts ‘puts Time.now.to_f - now’

Doesn’t the #{} force interpolation, meaning that the two test cases
are in fact the same?

Ray

Am Dienstag, den 21.03.2006, 10:28 -0800 schrieb Ray B.:

Doesn’t the #{} force interpolation, meaning that the two test cases
are in fact the same?

Ray

The line LINES.times { file.puts “foo = ‘#{rand_string}’” } only
generates code for the test ruby scripts.
If you look into the generated single_quotes.rb and double_quotes.rb
files, you will see it.


Norman T.

http://blog.inlet-media.de

Oleg F. wrote:

<%= javascript_include_tag ‘prototype’ %>

for we don’t need to substitute anything in this string. Or is it a
matter of style only in this case? If I stick to second way, will
templates be processed faster?

I am glad you asked this, because I’ve wondered about asking the
question myself and never got round to it.

My concern is about clarity of code, not performance. I feel that using
single quotes is a clear indication that there’s nothing clever going on
inside the string.

regards

Justin

Am Mittwoch, den 22.03.2006, 01:21 +0000 schrieb Justin F.:

Doesn’t the #{} force interpolation, meaning that the two test cases
are in fact the same?

If I understand your question…

irb(main):007:0> a = ‘Test #{15 + 3}’
=> “Test #{15 + 3}”
irb(main):008:0> b = “Test #{15 + 3}”
=> “Test 18”

Sure, but:

irb(main):001:0> puts “foo = ‘#{15 + 3}’”
foo = ‘18’
=> nil
irb(main):002:0> puts “foo = "#{15 + 3}"”
foo = “18”
=> nil

And this is what the above code is doing.


Norman T.

http://blog.inlet-media.de

Ray B. wrote:

Doesn’t the #{} force interpolation, meaning that the two test cases
are in fact the same?

If I understand your question…

irb(main):007:0> a = ‘Test #{15 + 3}’
=> “Test #{15 + 3}”
irb(main):008:0> b = “Test #{15 + 3}”
=> “Test 18”

Justin

Oleg F. wrote:

Hello everyone!

I’ve noticed, that the book uses double quotes almost everywhere. It is like

<%= javascript_include_tag “prototype” %>

I’ve checked the “programming ruby, 2ed” book and found that double
quotes are most suited when escape sequences and substitutions are
required. Isn’t it more semantically correct
No.
to write this code like

<%= javascript_include_tag ‘prototype’ %>

for we don’t need to substitute anything in this string. Or is it a
matter of style only in this case? If I stick to second way, will
templates be processed faster?

No. There will be a time difference only when you have an expression to
be interpolated inside the double quoted string.

– stefan


For rails performance tuning, see: RailsExpress.blog
Subscription: railsexpress.de

On Mar 23, 2006, at 4:04 AM, Oleg F. wrote:

My concern is about clarity of code, not performance. I feel that
using
single quotes is a clear indication that there’s nothing clever
going on
inside the string.

Yes, that’s what I feel too :slight_smile:

+1

I’ll bet we’re all just a bunch of old Perl hackers! :slight_smile:


– Tom M.

My concern is about clarity of code, not performance. I feel that using
single quotes is a clear indication that there’s nothing clever going on
inside the string.

Yes, that’s what I feel too :slight_smile:


olegf