Forum: Ruby on Rails Double and single quote usage in AWDWR

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Leo -. (Guest)
on 2006-03-21 10:43
(Received via mailing list)
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
Alex Y. (Guest)
on 2006-03-21 14:02
(Received via mailing list)
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...
Norman T. (Guest)
on 2006-03-21 14:53
(Received via mailing list)
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
Dave T. (Guest)
on 2006-03-21 15:04
(Received via mailing list)
>
> 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
Alex Y. (Guest)
on 2006-03-21 15:25
(Received via mailing list)
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. (Guest)
on 2006-03-21 16:18
(Received via mailing list)
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
Ray B. (Guest)
on 2006-03-21 20:45
(Received via mailing list)
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
Norman T. (Guest)
on 2006-03-21 21:05
(Received via mailing list)
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
Justin F. (Guest)
on 2006-03-22 03:22
(Received via mailing list)
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
Justin F. (Guest)
on 2006-03-22 03:26
(Received via mailing list)
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
Norman T. (Guest)
on 2006-03-22 10:33
(Received via mailing list)
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
Stefan K. (Guest)
on 2006-03-22 17:44
(Received via mailing list)
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: http://railsexpress.de/blog
Subscription: http://railsexpress.de/blog/xml/rss20/feed.xml
Leo -. (Guest)
on 2006-03-23 14:04
(Received via mailing list)
> 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 :)

--
olegf
Tom M. (Guest)
on 2006-03-23 19:49
(Received via mailing list)
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 :)

+1

I'll bet we're all just a bunch of old Perl hackers! :-)

--
-- Tom M.
This topic is locked and can not be replied to.