4

Why might you use ''' instead of """, as in Learn Ruby the Hard Way, Chapter 10 Study Drills?

animuson
  • 53,861
  • 28
  • 137
  • 147
  • You would not use either, likely. Not in Ruby. You can, but you probably shouldn't. There is nothing special going on here, and it may introduce newlines where you don't want them. Use Heredoc or perhaps even join a collection of strings with new lines. – vgoff Feb 18 '15 at 18:56
  • Single quotes in Ruby don't let you insert variables with `#{}`, double quotes do. – thejonanshow Nov 24 '20 at 00:57

4 Answers4

12

There are no triple quotes in Ruby.

Two String literals which are juxtaposed are parsed as a single String literal. So,

'Hello' 'World'
#=> "HelloWorld"

is the same as

'HelloWorld'
#=> "HelloWorld"

And

'' 'Hello' ''
#=> "Hello"

is the same as

'''Hello'''
#=> "Hello"

is the same as

'Hello'
#=> "Hello"

Since adding an empty string literal does not change the result, you can add as many empty strings as you want:

""""""""""""'''''Hello'''''''''
#=> "Hello"

There are no special rules for triple single quotes vs. triple double quotes, because there are no triple quotes. The rules are simply the same as for quotes.

Jörg W Mittag
  • 363,080
  • 75
  • 446
  • 653
  • This is entirely incorrect. See the example Ruby file using triple quotes below. – thejonanshow Nov 21 '20 at 07:08
  • 2
    If you are referring to [this answer](https://stackoverflow.com/a/64940658/2988), then no, the answer is incorrect. All of the examples in that answer are simply juxtaposition of multiple string literals, i.e. they are exactly what I am explaining in my answer. It is unfortunately hard to prove that something *doesn't* exist, but there is no mention of triple-quoted strings in the ISO Ruby Language Specification, the RubySpec, the YARV test suite, matz's *The Ruby Programming Language* book or any other book, in the Ruby documentation, or in the parser of any Ruby implementation. – Jörg W Mittag Nov 21 '20 at 09:44
  • Read the question, then read your answer. They're not asking your opinion on how it comes to pass that this works, they're asking what the difference is between single quotes and double quotes. It's about readability. – thejonanshow Nov 24 '20 at 00:47
10

I assume the author confused Ruby and Python, because a triple-quote will not work in Ruby the way author thought it would. It'll just work like three separate strings ('' '' '').

For multi-line strings one could use:

%q{
 your text
 goes here
}
 => "\n     your text\n     goes here\n    "

or %Q{} if you need string interpolation inside.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mmln
  • 2,104
  • 3
  • 24
  • 33
2

Triple-quotes ''' are the same as single quotes ' in that they don't interpolate any #{} sequences, escape characters (like "\n"), etc.

Triple-double-quotes (ugh) """ are the same as double-quotes " in that they do interpolation and escape sequences.

This is further down on the same page you linked.

The triple-quoted versions """ ''' allows for multi-line strings... as does the singly-quoted ' and ", so I don't know why both are available.

Satya
  • 4,458
  • 21
  • 29
-3

In Ruby """ supports interpolation, ''' does not.

Rubyists use triple quotes for multi-line strings (similar to 'heredocs').

You could just as easily use one of these characters.

Just like normal strings the double quotes will allow you to use variables inside of your strings (also known as 'interpolation').

Save this to a file called multiline_example.rb and run it:

interpolation = "(but this one can use interpolation)"

single = '''
This is a multi-line string.
'''

double = """
This is also a multi-line string #{interpolation}.
"""

puts single
puts double

This is the output:

$ ruby multiline_string_example.rb

This is a multi-line string.

This is also a multi-line string (but this one can use interpolation).

$

Now try it the other way around:

nope = "(this will never get shown)"

single = '''
This is a multi-line string #{nope}.
'''

double = """
This is also a multi-line string.
"""

puts single
puts double

You'll get this output:

$ ruby multiline_example.rb

This is a multi-line string #{nope}.

This is also a multi-line string.

$

Note that in both examples you got some extra newlines in your output. That's because multiline strings keep any newlines inside them, and puts adds a newline to every string.

thejonanshow
  • 131
  • 1
  • 5
  • 1
    Triple quotes aren't a thing in Ruby; `'''...'''` is just two empty strings and a single-quoted string automatically concatenated together, same as writing `'' + '...' + ''`; similarly for double quotes. Ruby heredocs use `< – mu is too short Nov 21 '20 at 18:45
  • Restated the question is "What's the difference between single quotes and double quotes in Ruby?" My answer: "Interpolation." Other answer: "Empty string literals do not change the result." One of these is actually answering the question. – thejonanshow Nov 24 '20 at 00:53
  • 1
    But your answer is still talking about tripled quotes. And if you think it is about `'` vs `"` then it would be a duplicate of [**Double vs single quotes**](https://stackoverflow.com/q/6395288/479863) (and several others). – mu is too short Nov 26 '20 at 23:41
  • "There are no triple quotes in Ruby." This is the nature of my objection I suppose: there are triple quotes in Ruby. You can type them, and it is Ruby, and it runs. You can argue about abstract syntax trees and parsers all you want but fundamentally you're just showing up to someone with a genuine question of "why does this work?" with "it doesn't", which is clearly does. – thejonanshow Dec 27 '20 at 05:05
  • 1
    There are no triple quotes in Ruby in the same way that there is no [`!=~`](https://stackoverflow.com/q/7693486/479863), [`==~`](https://stackoverflow.com/q/63095137/479863), and no [`+->`, `-->`, `!->`, and `~->`](https://stackoverflow.com/q/7816444/479863). Sure, they all work but they're all other things combined and written in misleading ways. – mu is too short Dec 27 '20 at 17:25
  • In which case perhaps your answer to "why are these different?" should be "interpolation, also I feel it necessary to note that characters in this language should only be used according to my personal preferences." – thejonanshow Dec 28 '20 at 22:18