String Interpolation क्या है
String interpolation आपको एक value एक sentence के अंदर ही drop करने देता है, एक story में एक blank भरने की तरह। Ruby marker को actual value से swap करता है।
In this page:
Syntax
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
name = "Ruby"
version = 3.2
puts "Hello, #{name}! Version: #{version}"
puts "2 + 2 = #{2 + 2}"
puts 'No interpolation here: #{name}'
Login to try C/C++/Java/PHP code in the editor
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. #}
आम गलतियां
'Hello, #{name}'जैसे single quotes इस्तेमाल करना, जो#{name}literally print करता है।- braces भूल जाना, जैसे
"Hello #name", जो#nameprint करता है। "Total: " + 5जैसे+से एक number concatenate करना, जोTypeErrorraise करता है;#{5}से interpolate करें।
चैप्टर सारांश
- Ruby install करने के बाद आप इसे IRB से या scripts की तरह चला सकते हैं।
puts,print, औरpoutput 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: