← Back to Ruby Course | Chapter 1: Setup & Basics | Lesson 7 of 7

String Interpolation क्या है

String interpolation आपको एक value एक sentence के अंदर ही drop करने देता है, एक story में एक blank भरने की तरह। Ruby marker को actual value से swap करता है।

In this page:

  1. String Interpolation
Syntax
ruby
name = "value"
puts "text #{name} more text"
puts "sum: #{expression}"

String Interpolation

Ruby आपको #{expression} से directly एक double-quoted string के अंदर expressions embed करने देता है, जो expression evaluate करता है और इसका string form insert करता है।

यह सिर्फ double-quoted strings में काम करता है, single-quoted में नहीं। Interpolation strings बनाने का idiomatic Ruby तरीका है, generally + से concatenation से preferred।

Warning: String interpolation सिर्फ double quotes के अंदर काम करता है -- single quotes के साथ 'Hello #{name}' literal text print करता है, interpolated value नहीं।

उदाहरण: String Interpolation

markup
name = "Ruby"
version = 3.2

puts "Hello, #{name}! Version: #{version}"
puts "2 + 2 = #{2 + 2}"
puts 'No interpolation here: #{name}'
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. 'Hello, #{name}' जैसे single quotes इस्तेमाल करना, जो #{name} literally print करता है।
  2. braces भूल जाना, जैसे "Hello #name", जो #name print करता है।
  3. "Total: " + 5 जैसे + से एक number concatenate करना, जो TypeError raise करता है; #{5} से interpolate करें।
चैप्टर सारांश
  • Ruby install करने के बाद आप इसे IRB से या scripts की तरह चला सकते हैं।
  • puts, print, और p output display करते हैं, और comments code document करते हैं।
  • String interpolation text में values insert करता है।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.