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

Heredoc क्या है

एक heredoc दो markers के बीच text का एक लंबा block लिखने का तरीका है, एक start और end line के बीच रखी एक letter की तरह। यह बड़े text pieces के लिए layout tidy रखता है।

In this page:

  1. Heredoc
Syntax
ruby
text = <<~IDENTIFIER
  first line
  second line
IDENTIFIER
puts text

Heredoc

<<~IDENTIFIER से शुरू और अपनी line पर एक matching IDENTIFIER से खत्म होने वाला एक heredoc, आपको multi-line text का एक बड़ा, readable block लिखने देता है।

~ variant (squiggly heredoc) automatically leading indentation strip करता है, heredoc को surrounding code के साथ nicely indented रखना आसान बनाते हुए। Heredocs double-quoted strings जैसे string interpolation support करते हैं।

Note: older <<- से <<~ (squiggly heredoc) prefer करें क्योंकि यह automatically common leading whitespace strip करता है।

उदाहरण: Heredoc

markup
name = "World"

text = <<~GREETING
  Hello, #{name}!
  This is a heredoc.
  It can span many lines.
GREETING

puts text
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. <<- या << इस्तेमाल करना और indentation हटने की उम्मीद करना, जब सिर्फ <<~ common indent strip करता है।
  2. closing identifier को <<~ की बजाय << से indent करना, इसलिए यह recognised नहीं होता।
  3. identifier को <<~EOS की तरह quote करना और #{name} के काम करने की उम्मीद करना, जब यह interpolation रोकता है।
🔒

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.