8 Strings, symbols, and other scalar objects
This chapter explains the main scalar objects Ruby uses to represent text, numbers, and time-related data. It begins by contrasting scalar objects with collections, then focuses on the two text-oriented classes, strings and symbols. Strings are Ruby’s flexible, mutable text type, while symbols are immutable, unique identifiers that often act like compact labels. The chapter also introduces integers, floating-point numbers, and date/time objects as other important scalar values, emphasizing that each behaves as a rich object with its own methods and rules.
The discussion of strings is especially detailed, covering how to create them, how quotation style affects interpolation and escaping, and how alternate forms such as percent-based quoting and heredocs work. It then shows how to inspect and manipulate string content through indexing, slicing, replacement, concatenation, interpolation, and boolean/content queries. The chapter also explores transformations like case changes, justification, trimming, chopping, deleting, clearing, replacing, encrypting, and incrementing strings, as well as conversion methods for turning strings into numbers, symbols, and other types. Encoding gets special attention too, since Ruby strings are byte sequences whose behavior depends on character encoding and transcoding.
Symbols are presented as a practical counterpart to strings: they are immutable, internally unique, and closely tied to Ruby’s identifier system. The chapter shows how they are created, how Ruby stores and reuses them, and why they are common as method arguments and hash keys. It then moves to numeric objects, reminding readers that arithmetic in Ruby is method-based and that integers and floats follow familiar mathematical rules, including base-specific representations and integer division behavior. Finally, the chapter introduces time and date classes, demonstrating how to create, query, format, convert, and move among Time, Date, and DateTime objects, before closing with the broader idea that these scalar classes are foundational tools for everyday Ruby programming.
Summary
- Strings and symbols both represent text, and although they’re different kinds of objects, conversions from one to the other are easy and common.
- Numbers and strings interact, too. Ruby supplies conversion methods to go from string to numerical object and back, as well as ways to convert strings to integers in as many bases as 10 digits and the 26 letters of the English alphabet can accommodate.
- You can perform calculations on time and date objects, such as adding n months to a given date, and you can also put them through their paces as strings, using techniques like the Time#strftime method in conjunction with output format specifiers.
FAQ
What does “scalar” mean in Ruby?
Scalar objects represent a single value rather than a collection. Examples include strings, integers, floats, symbols, and time/date objects.What is the difference between single-quoted and double-quoted strings?
Single-quoted strings do not support interpolation and treat many backslashes literally. Double-quoted strings support interpolation and special escapes like\n and \t.How do I write strings in Ruby besides using regular quotes?
Ruby supports percent-style quoting such as%q{} for single-quoted behavior and %Q{} or %{} for double-quoted behavior. It also supports heredocs for multiline strings.What is a heredoc and when should I use it?
A heredoc is a multiline string introduced with<<. It is useful for large text blocks, templates, and embedded data such as SQL queries.How do I get or replace part of a string?
You can use[] to fetch substrings and []= to replace them. Ruby supports numeric indexes, ranges, strings, and regular expressions for locating substrings.What is the difference between + and << for strings?
+ creates a new string and leaves the original unchanged. << appends to the original string in place.How do I test whether a string contains something?
Use methods likeinclude?, start_with?, end_with?, and empty?. These return true or false based on the string’s current content.How are strings transformed in Ruby?
Ruby offers case transformations such asupcase, downcase, and capitalize; formatting methods like ljust, rjust, and center; and content methods like chop, chomp, delete, and succ.What makes symbols different from strings?
Symbols are immutable and unique, while strings are mutable and each literal creates a new object. Symbols are often used for method arguments and hash keys because they are efficient and have a label-like meaning.How do Ruby’s time and date objects work?
Ruby providesTime, Date, and DateTime objects. You can create them with constructors like new, today, and parse, query fields such as year and month, format them with strftime, and convert between the classes.
The Well-Grounded Rubyist, Fourth Edition ebook for free