Triple Single Quote VS Triple Double Quote in Ruby

Learn Ruby The Hard Way Chapter 9 Triple Quotes

I don't know why he uses triple double quotes in his book. They're nothing special, and one double quote works just fine.

This is a little known "feature" of ruby - it simply glues adjacent strings together.

s = "hello " "world" # equivalent to "hello " + "world"
s # => "hello world"

So your example is equivalent to

puts "" + "
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
" + ""

More string tricks: http://pivotallabs.com/stupid-ruby-quoting-tricks/

Ruby remove triple quotes

This one may help:

marks = 5
csv=CSV.open("temp.csv", "w", {quote_char: " "})
csv << ["\"marks\"", marks]
csv.close

csv in editor

Difference between '..' (double-dot) and '...' (triple-dot) in range generation?

The documentation for Range says this:

Ranges constructed using .. run from the beginning to the end inclusively. Those created using ... exclude the end value.

So a..b is like a <= x <= b, whereas a...b is like a <= x < b.


Note that, while to_a on a Range of integers gives a collection of integers, a Range is not a set of values, but simply a pair of start/end values:

(1..5).include?(5)           #=> true
(1...5).include?(5) #=> false

(1..4).include?(4.1) #=> false
(1...5).include?(4.1) #=> true
(1..4).to_a == (1...5).to_a #=> true
(1..4) == (1...5) #=> false



The docs used to not include this, instead requiring reading the Pickaxe’s section on Ranges. Thanks to @MarkAmery (see below) for noting this update.

unescape_javascript() vs Coffeescript triple quotes

Triple quotes in CoffeeScript don't escape anything, they just handle interpolation, strip off some leading whitespace, and convert embedded newlines to \n; of course, escape_javascript doesn't do anything with HTML either. For example:

s = """
<p>
Pancakes & stuff!
'single quotes'
"double quotes"
</p>
"""

becomes:

var s;
s = "<p>\n Pancakes & stuff!\n 'single quotes'\n \"double quotes\"\n</p>";

Single quotes have no meaning inside double quoted strings so CoffeeScript does nothing with them, double quotes are escaped though. But, an embedded triple quote will be interpreted as the end of the triple quoted string so, if your ERB came before your CoffeeScript to JavaScript translation, you could run into some trouble.

I would recommend against dropping escape_javascript in favor of throwing raw strings into CoffeeScript triple-quoted strings. If you really want to use triple-quoted strings then you'd do both:

s = """<%=j @s %>"""

but that would be a pointless use of """ since """ and escape_javascript do pretty much the same thing (except that """ will be confused by an embedded """ of course).

Ruby quotes, why am I seeing triple double quotes when I save this CSV to a file?

My guess would be that fastercsv is adding the extra quotes to escape the quotes in your input string.
So if you're input string is [Hello, CSV], faster csv would have to enclose it within double quotes so that csv parsing isn't disrupted by the comma. Ditto for double quotes which have significance in CSV.

I'd say try sending string without the quotes, let fastercsv decide when it needs the double quotes OR use single quotes like Jacob suggests.

How do I declare a string with both single and double quotes in YAML?

escaping should be done like this

"When you're using double quotes, they look like \"this\""

What is the standard for use of quotes in Typescript?

There is no particular standard to use single quotes for characters and double quotes for string but it is suggested to use double quotes for strings and vice versa.

From the docs:

Just like JavaScript, TypeScript also uses the double quote (") or
single quote (') to surround string data.

Is there a performance gain in using single quotes vs double quotes in ruby?

$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.0.0]

$ cat benchmark_quotes.rb
# As of Ruby 1.9 Benchmark must be required
require 'benchmark'

n = 1000000
Benchmark.bm(15) do |x|
x.report("assign single") { n.times do; c = 'a string'; end}
x.report("assign double") { n.times do; c = "a string"; end}
x.report("concat single") { n.times do; 'a string ' + 'b string'; end}
x.report("concat double") { n.times do; "a string " + "b string"; end}
end

$ ruby benchmark_quotes.rb

user system total real
assign single 0.110000 0.000000 0.110000 ( 0.116867)
assign double 0.120000 0.000000 0.120000 ( 0.116761)
concat single 0.280000 0.000000 0.280000 ( 0.276964)
concat double 0.270000 0.000000 0.270000 ( 0.278146)

Note: I've updated this to make it work with newer Ruby versions, and cleaned up the header, and run the benchmark on a faster system.

This answer omits some key points. See especially these other answers concerning interpolation and the reason there is no significant difference in performance when using single vs. double quotes.



Related Topics



Leave a reply



Submit