Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6045607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T07:08:37+00:00 2026-05-23T07:08:37+00:00

long_string = <<EOS It was the best of times, It was the worst of

  • 0
long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS

that returns 53. Why? The whitespace counts? Even still. how do we get 53?

How about this?

     def test_flexible_quotes_can_handle_multiple_lines
    long_string = %{
It was the best of times,
It was the worst of times.
}
    assert_equal 54, long_string.size
  end

  def test_here_documents_can_also_handle_multiple_lines
    long_string = <<EOS
It was the best of times,
It was the worst of times.
EOS
    assert_equal 53, long_string.size
  end

Is this the case because the %{ case counts each /n as one character and theres considered to be one before the first line, one at the end, and then at the end of the 2nd line, whereas in the EOS case theres just one before the 1st line and one after the 1st line? In other words, why is the former 54 and the latter 53?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T07:08:38+00:00Added an answer on May 23, 2026 at 7:08 am

    For:

    long_string = <<EOS
    It was the best of times,
    It was the worst of times.
    EOS
    
    String is:
    "It was the best of times,\nIt was the worst of times.\n"
    
    It was the best of times, => 25
    <newline> => 1
    It was the worst of times. => 26
    <newline> => 1
    Total = 25 + 1 + 26 + 1 = 53
    

    And

    long_string = %{
    It was the best of times,
    It was the worst of times.
    }
    
    String is:
    "\nIt was the best of times,\nIt was the worst of times.\n"
    #Note leading "\n"
    

    How it works:

    In the case of <<EOS, the lines that follow it are part of the string. All the text after << on the same line as << and to the end of the line would be part of the “marker” that determines when the string ends (in this case an EOS on a line by itself is matching the <<EOS).

    In case of %{...}, it is just a different delimiter used in place of "...". So when you have the string starting on a new line after a %{, that newline is part of the string.

    Try this example and you will see how %{...} is working same as "...":

    a = "
    It was the best of times,
    It was the worst of times.
    "
    a.length # => 54
    
    b = "It was the best of times,
    It was the worst of times.
    "
    b.length # => 53
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some rather long string containing just about anything that I want to
I'd like to do something like this: boost::regex re(tryme); ifstream iss; iss.open(file.txt); istream_iterator<string> eos;
I have a long string that contains information like this: |infoId-*-info||infoId-*-info||infoId-*-info| ... I use
i have a long string of date . looks like that : Mon Jun
I have a long string of comments that I'd like to split into multiple
I have a long string, ex: Please help me to solve this problem. This
I have a long string (a DNA sequence). It does not contain any whitespace
I am trying to write a long string in Python that gets displayed as
I was learning ruby through ruby koans when I faced these two functions: def
I have a somewhat long string that gets truncated when displayed in a tablView

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.