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

  • Home
  • SEARCH
  • 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 3950516
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:37:15+00:00 2026-05-20T01:37:15+00:00

ARGF.each_with_index do |line, idx| print( [#{idx}] #{line} x: ); indent = Indent # do

  • 0
   ARGF.each_with_index do |line, idx|
      print( "[#{idx}] #{line} x: " );
      indent = Indent
         # do stuff
      indent = ""
   end #ARGF e

Each line from STDIN is displayed as,

   x: [1] W:\sandbox\tmp\for_each\for_each.rake

Which is not good / odd, if you ask me.

One further unexplained result, the line …

 print( "ab: [#{idx}] #{line} x: " );

Displays:

x: ab: [1] W:\sandbox\tmp\for_each\for_each.rake

I just get weird feelings here. Three questions, is this a:

  1. HOW does the “x:” string wind-up at the beginning of each ‘line‘ the string??!
  2. Ruby v1.8.7 bug or my bug?
  3. How to fix it?

Help wanted. I keep looking at this and wonder if I what really dumb thing I did? Many thanks in advance.

aloha, Will

  • 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-20T01:37:15+00:00Added an answer on May 20, 2026 at 1:37 am

    each_line does not remove the newlines at the end of the lines, so the strings yielded by each_line will contain newlines. print on the other hand will not add newlines to the end of strings (if you want that, use puts). So print "hello\nworld: followed by print "lala", will print

    hello
    world:lala
    

    which explains why your output looks the way it does.

    To get the output you want, use line.chomp instead of line, which will remove the newline at the end of line and thus prevent your strings from containing a line break before x:.

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

Sidebar

Related Questions

I have this code if filename begin if filename == '-' ARGF.each{|url| begin check(url)
enter code here This is a one liner to randomize STDIN, the aim is
(I asked a variation of this question on comp.std.c++ but didn't get an answer.)
This is a follow-on question to C++0x rvalue references and temporaries In the previous
David Korn, a proponent of the Unix philosophy, chided Perl programmers a few years
I want to write a Module Arg[f_,n_] that takes a function f (having <=n
What's wrong with the below use of rest params with defprotocol and defrecord in
This question is kinda simple (don't be so harsh with me), but I can't
EDIT: Please, please , please read the two requirements listed at the bottom of
I have a script called jobrunner.py that calls class methods in main.py. See below...

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.