← Back to Ruby Course | Chapter 3: Strings | Lesson 2 of 7

Multiline Strings कैसे लिखें

एक multiline string आपको text लिखने देता है जो कई lines में फैलता है exactly जैसे आप type करें, एक page पर एक poem की तरह। आपको special line-break marks की ज़रूरत नहीं।

In this page:

  1. Multiline Strings
Syntax
ruby
text = "first line
second line"
puts text

Multiline Strings

एक Ruby string literal quotes के अंदर directly कई lines में फैल सकता है, line breaks को string के content के हिस्से की तरह preserve करते हुए। यह short multi-line text के लिए explicit \n characters इस्तेमाल करने से simpler है।

लंबे text blocks के लिए, एक heredoc (अगला cover होगा) आमतौर पर ज़्यादा readable है।

Note: एक quoted string के अंदर सीधे type किए line breaks resulting string में literal newline characters बन जाते हैं।

उदाहरण: Multiline Strings

markup
message = "Line one
Line two
Line three"

puts message
puts message.lines.count
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. quotes के अंदर continuation lines में leading spaces add करना और output में इनकी उम्मीद न करना।
  2. quote close करना भूल जाना, इसलिए बाकी file string का हिस्सा treat होती है।
  3. split("\n") से lines count करना जब lines newline characters रखता है।
🔒

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.