Strings in Ruby
Master Ruby strings with interpolation, heredocs, frozen string literals, and essential methods like split, gsub, strip, upcase, and format.
Creating Strings
Ruby strings can be created with single quotes, double quotes, heredocs, or the `%q`/`%Q` literals.
1. Single vs Double Quotes
Single-quoted strings are literal. Double-quoted strings support interpolation and escape sequences.
2. Heredoc — Multi-line Strings
`<<~HEREDOC` creates a multi-line string with proper indentation stripping.
3. %q and %Q Literals
`%q` is like single quotes; `%Q` (or just `%`) is like double quotes — useful when your string contains many quotes.
String Basics (Indexing, Length, Traversal)
Ruby strings are sequences of characters. You can index them, measure their length, and traverse them character by character.
1. Length & Size
`length` and `size` are aliases — both return the number of characters.
2. Indexing Characters
Access individual characters using `[]` with an index. Negative indices count from the end.
3. Traversing a String
Use `each_char` to iterate over every character, or `chars` to get an array of characters.
Formatted Output
Ruby supports string interpolation `#{}`, `sprintf`/`format`, and the `%` operator for printf-style formatting.
1. String Interpolation
Embed expressions directly in strings with `#{}`.
2. format / sprintf
Use `format` (alias: `sprintf`) for printf-style string templates.
3. % Operator
The `%` operator on strings works like `sprintf` with a cleaner syntax.
Comparing & Concatenating Strings
Ruby provides `+` and `<<` for concatenation, and `==`, `<=>`, and `eql?` for comparison.
1. Concatenation
`+` creates a new string; `<<` (shovel) mutates in place and is faster.
2. String Comparison
Use `==` for equality and `<=>` (spaceship) for ordering.
String Transformations
Ruby strings have a rich set of transformation methods: case conversion, stripping whitespace, reversing, and more.
1. Case Conversion
Convert strings to upper, lower, title, or swapped case.
2. Strip Whitespace
`strip`, `lstrip`, and `rstrip` remove leading and/or trailing whitespace.
3. Reverse & Repeat
`reverse` flips a string; `*` repeats it.
4. Replace with gsub / sub
`gsub` replaces all occurrences; `sub` replaces only the first.
ASCII & Unicode Conversions
Ruby provides `ord` to get a character's code point and `chr` to convert a code point back to a character.
1. ord and chr
`ord` returns the Unicode code point of a character; `chr` converts it back.
Slicing, Splitting & Trimming
Ruby provides `[]`, `slice`, `split`, `chomp`, and `chop` to extract and divide string content.
1. Slicing with [] and slice
Extract a substring using a range or start/length notation.
2. Splitting
`split` divides a string into an array using a delimiter.
3. chomp, chop, and strip
`chomp` removes trailing newline, `chop` removes the last character, `strip` removes surrounding whitespace.
Searching & Replacing
Ruby provides `include?`, `start_with?`, `end_with?`, `index`, `match?`, and `scan` for searching inside strings.
1. include?, start_with?, end_with?
Check for the presence or position of a substring.
2. index and rindex
`index` finds the first occurrence; `rindex` finds the last.
3. scan — Find All Matches
`scan` returns an array of all substrings matching a pattern.