Ruby:
Інтерполяція рядків
How to: (Як це зробити:)
name = "Іван"
greeting = "Привіт, #{name}!"
puts greeting # Виведе: Привіт, Іван!
Compound example with calculations:
hours_worked = 9
rate = 50
puts "Сьогодні ти заробив: #{hours_worked * rate} гривень." # Виведе: Сьогодні ти заробив: 450 гривень.
Deep Dive (Поглиблений Розгляд)
Ruby first introduced string interpolation in version 1.8. It’s cleaner than concatenation which uses ‘+’:
# Without interpolation
puts 'Привіт, ' + name + '!'
You can’t interpolate with single quotes, only double quotes or backticks:
# Won't work
puts 'Hello, #{name}!'
Interpolation automatically calls to_s
on the variable:
# Even if name were a number, it'd convert it to a string:
name = 123
puts "Привіт, #{name}" # Виведе: Привіт, 123
Finally, when it comes to performance, interpolation is generally faster than concatenation.
See Also (Дивіться також)
- The Ruby documentation provides details on string interpolation: https://ruby-doc.org/core-3.1.0/doc/syntax/literals_rdoc.html#label-Strings
- Practical use of string interpolation: https://www.rubyguides.com/2019/05/ruby-string-interpolation/
- For best practices when using string interpolation and concatenation: https://www.rubyguides.com/2020/01/ruby-string-concatenation/