Strings

Contents
Double vs single quotes
Interpolation #{}
Heredocs
Handy methods

Double vs single quotes

Strings can sit in double or single quotes, and for plain text both behave the same. Double quotes additionally process escape sequences such as newline, while single quotes keep almost everything literally.

a = "hi" b = 'hi' puts "a\nb" # prints a, then b on a new line puts 'a\nb' # prints a\nb literally

Interpolation #{}

Inside double quotes you can embed any expression with a hash mark and curly braces. Ruby runs the code inside the braces and pastes the result into the string.

name = "Ada" puts "Hello, #{name}" # prints Hello, Ada puts "2 + 2 = #{2 + 2}" # prints 2 + 2 = 4

Interpolation works only in double quotes. In single quotes the same braces stay untouched, which is a classic beginner surprise.

Heredocs

Long multi-line text is easier to read as a heredoc than as many quoted lines. Open with two angle signs and a label, write any lines you want, then close with the same label on its own line.

text = <<~TEXT Hello, Ruby. TEXT puts text

Hello,
Ruby.

Handy methods

Strings come with many small helpers. length counts characters, upcase builds a loud copy, strip trims spaces on both sides, and split cuts a string into an array of pieces.

"Ada".length # => 3 "ada".upcase # => "ADA" " Ada ".strip # => "Ada" "a,b".split(",") # => ["a", "b"]

Next: Symbols

Article author: Arthur Isaev

Related articles
Ruby
Ruby course: zero to hero in 50 lessons
What is Ruby and where it runs
Install Ruby and check the version
irb: the interactive shell
Your first Ruby script
Variables
Numbers
Strings
Symbols
Truthiness: only false and nil
Conditions
Loops
Arrays
Hashes
Ranges
Methods
Blocks, procs, lambdas
String tools
Files and IO
Exceptions
Modules and mixins
Classes and objects
Inheritance
Enumerables
Duck typing and core idioms
Gems and Bundler
Testing with Minitest
Debugging
Encodings
Time and date
JSON and YAML
HTTP clients
Sockets
REST APIs
puts, print and p
Metaprogramming
Enumerators
Pattern matching
Arguments: splats and kwargs
Rake tasks
Environment and CLI
Logging
CSV
ERB templates
WEBrick server
Rails in one lesson
Capstone: todo CLI
Capstone: JSON API
Performance
Modern Ruby tour
Hero roadmap: the whole course on one page
Install Ruby on Windows 11
Install Ruby on Ubuntu
Install Ruby on Rocky Linux
Install Ruby on macOS
Install Ruby on FreeBSD
Ruby in Docker
Threads and fibers

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.ru
Write to info@urn.su, info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: