String Interpolation & Manipulation
Master Ruby's powerful string features. Learn interpolation, escape sequences, heredocs, and essential string manipulation methods.
String Interpolation
Interpolation: Use #{} to embed Ruby expressions inside double-quoted strings.
Basic Interpolation Examples
Escape Sequences
Escape Sequences: Special characters that start with backslash (\) to represent non-printable characters or special meanings.
Common Escape Sequences
Unicode Escapes
Heredocs - Multi-line Strings
Heredocs: A way to write multi-line strings that preserve formatting and support interpolation.
Heredoc Examples
Essential String Methods
Case Methods
Search Methods
Manipulation Methods
Formatting Methods
String Comparison & Equality
String Comparison Examples
Best Practices
String Best Practices
- Use double quotes for interpolation: Single quotes are literal
- Prefer heredocs for multi-line strings: Better readability
- Use strip for user input: Remove leading/trailing whitespace
- Be careful with string mutability: Methods return new strings
- Use appropriate comparison methods: == vs eql? vs equal?
Common Mistakes to Avoid
- Forgetting string immutability: Methods don't modify the original
- Using single quotes for interpolation: Won't work as expected
- Not handling nil in interpolation: Can cause errors
- Ignoring encoding issues: Be aware of character encoding
- Overusing string concatenation: Use interpolation instead
Practice Exercises
1 Basic String Interpolation
Create a greeting message using string interpolation:
2 Math in Strings
Perform calculations inside string interpolation:
3 Method Calls in Interpolation
Call methods inside string interpolation:
4 Complex Interpolation
Create a detailed report using multiple interpolations:
Try It Yourself
Practice Makes Perfect! Try these exercises in the code runner below:
- Create a personal introduction using your name and age
- Build a shopping receipt with item prices and totals
- Make a weather report with temperature and conditions
- Create a student report card with grades and averages
Try It Yourself
Now practice what you've learned! Use the code editor below to try the exercises above and experiment with string interpolation.
- Start with the practice exercises above - try each one!
- Remember: use
#{}for interpolation, not${} - You can do math inside interpolation:
puts "Total: #{10 + 5}" - Call methods inside interpolation:
puts "Name: #{name.upcase}"
What's Next?
Continue Your Ruby Journey
- Learn Symbols: Understand Ruby's symbol system and immutability
- Explore Regular Expressions: Master pattern matching with strings
- Study Collections: Learn about arrays and hashes
- Practice String Methods: Build projects using string manipulation
- Advanced Topics: Explore encoding, internationalization, and performance
Try It Yourself - Interactive Practice
Learning Tip: The best way to master string interpolation is through hands-on practice! Try these examples and experiment with different variables and expressions!