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 6932285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:41:12+00:00 2026-05-27T11:41:12+00:00

This completely confused me for a while. I’m getting weird behaviour trying to loop

  • 0

This completely confused me for a while. I’m getting weird behaviour trying to loop through lines of text in erb, with the whole line of text being printed when the loop is complete.

<% "some\nmultiline\ntext".each_line do |line| %>
  <%= line %> <br />
<% end %>

Outputs:

some 
multiline 
text 
some multiline text

And so does:

<% "some\nmultiline\ntext".lines.each do |line| %>
  <%= line %> <br />
<% end %>

But the following works as I would expect it to:

<% "some\nmultiline\ntext".lines.to_a.each do |line| %>
  <%= line %> <br />
<% end %>

and prints:

some 
multiline 
text 

I’m definitely not just putting in an = accidentally. What could be causing this strange behaviour?

I’m using Rails 3.0.10, Ruby 1.9.2.

  • 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-27T11:41:12+00:00Added an answer on May 27, 2026 at 11:41 am

    I can’t say for sure but "some\nmultiline\ntext".lines is Enumerator and "some\nmultiline\ntext".lines.to_a is Enumerable the big difference is that when you mix in Enumerable module to your class you have to define each method whose job it is to
    yield items to a supplied code block, one at a time. Enumerators are a recent and significant addition to Ruby. An enumerator is an object, not a method. An enumerator isn’t a container object. It has no “natural” basis for an each operation, the way an array does (start at element 0; yield it; go to element 1; yield it; and so on). The each iteration logic of every enumerator has to be explicitly specified.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm getting really confused on how to do this whole thing and I
I have completely confused myself trying to design the necessary interfaces and abstracts to
I'm reading through the TAP Whitepaper , and am confused by this sample given
Now, I know this is completely subjective, so please don't flame me. I've never
I realize that this would be COMPLETELY bad practice in normal situations, but this
Seems to me most of developers completely ignore this features. People prefer handling security
I have a feeling I am completely borking this MySQL query but I'll ask
I have really no idea why I'm asking this as this a really completely
I am completely stumped as to why this code does not get any SDL
I'm completely stumped on this one. I have three different lists that need to

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.