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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:04:51+00:00 2026-05-26T00:04:51+00:00

I’m fairly new to Ruby and I’ve been searching Google for a few hours

  • 0

I’m fairly new to Ruby and I’ve been searching Google for a few hours now.
Does anyone know how to format the output of a print to be no more than 40 characters long?

For example:

What I want to print:

This is a simple sentence.
This simple
sentence appears
on four lines. 

But I want it formatted as:

This is a simple sentence. This simple
sentence appears on four lines.

I have each line of the original put into an array.
so x = ["This is a simple sentence.", "This simple", "sentence appears", "on three lines."]
I tried x.each { |n| print n[0..40], " " } but it didn’t seem to do anything.

Any help would be fantastic!

  • 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-26T00:04:52+00:00Added an answer on May 26, 2026 at 12:04 am

    The method word_wrap expects a Strind and makes a kind of pretty print.

    Your array is converted to a string with join("\n")

    The code:

    def word_wrap(text, line_width = 40 ) 
      return text if line_width <= 0
      text.gsub(/\n/, ' ').gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip
    end
    
    x = ["This is a simple sentence.", "This simple", "sentence appears", "on three lines."]
    
    puts word_wrap(x.join("\n"))
    x << 'a' * 50 #To show what happens with long words
    x << 'end'
    puts word_wrap(x.join("\n"))
    

    Code explanation:

    x.join("\n")) build a string, then build one long line with text.gsub(/\n/, ' ').
    In this special case this two steps could be merged: x.join(" "))

    And now the magic happens with

    gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n")
    
    • (.{1,#{line_width}})): Take any character up to line_width characters.
    • (\s+|$): The next character must be a space or line end (in other words: the previous match may be shorter the line_width if the last character is no space.
    • "\\1\n": Take the up to 40 character long string and finish it with a newline.
    • gsub repeat the wrapping until it is finished.

    And in the end, I delete leading and trailing spaces with strip

    I added also a long word (50 a’s). What happens? The gsub does not match, the word keeps as it is.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
Does anyone know how can I replace this 2 symbol below from the string
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.