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.
06e1b396407b733b4b3d825cb2c57856?d=identicon&s=25 Leo -- (leopardus)
on 2006-03-21 09: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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-21 13:02
(Received via mailing list)
Oleg Frolov 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...
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-21 13:53
(Received via mailing list)
Am Dienstag, den 21.03.2006, 11:57 +0000 schrieb Alex Young:
> >
> > <%= 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 Timmler

http://blog.inlet-media.de
5d06917e13b29bcff1c1609492c06873?d=identicon&s=25 Dave Thomas (Guest)
on 2006-03-21 14: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
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (Guest)
on 2006-03-21 14:25
(Received via mailing list)
Norman Timmler 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
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-21 15:18
(Received via mailing list)
Am Dienstag, den 21.03.2006, 13:24 +0000 schrieb Alex Young:
> Norman Timmler 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 Timmler

http://blog.inlet-media.de
E28c35323f624b8b9ed8712e25105454?d=identicon&s=25 Ray Baxter (Guest)
on 2006-03-21 19:45
(Received via mailing list)
Norman Timmler 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
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-21 20:05
(Received via mailing list)
Am Dienstag, den 21.03.2006, 10:28 -0800 schrieb Ray Baxter:
> 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 Timmler

http://blog.inlet-media.de
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-22 02:22
(Received via mailing list)
Ray Baxter 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
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2006-03-22 02:26
(Received via mailing list)
Oleg Frolov 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
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-22 09:33
(Received via mailing list)
Am Mittwoch, den 22.03.2006, 01:21 +0000 schrieb Justin Forder:
> >
> > 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 Timmler

http://blog.inlet-media.de
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 Stefan Kaes (Guest)
on 2006-03-22 16:44
(Received via mailing list)
Oleg Frolov 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
06e1b396407b733b4b3d825cb2c57856?d=identicon&s=25 Leo -- (leopardus)
on 2006-03-23 13: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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-03-23 18:49
(Received via mailing list)
On Mar 23, 2006, at 4:04 AM, Oleg Frolov 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 Mornini
This topic is locked and can not be replied to.