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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:14:54+00:00 2026-06-14T21:14:54+00:00

How can I write a Ruby function that splits the input by any kind

  • 0

How can I write a Ruby function that splits the input by any kind of whitespace, and remove all the whitespace from the result? For example, if the input is

 aa bbb
cc    dd ee

Then return an array ["aa", "bbb", "cc", "dd", "ee"].

  • 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-06-14T21:14:55+00:00Added an answer on June 14, 2026 at 9:14 pm

    The following should work for the example you gave:

    str.gsub(/\s+/m, ' ').strip.split(" ")
    

    it returns:

    ["aa", "bbb", "cc", "dd", "ee"]
    

    Meaning of code:

    /\s+/m is the more complicated part. \s means white space, so \s+ means one ore more white space letters. In the /m part, m is called a modifier, in this case it means, multiline, meaning visit many lines, not just one which is the default behavior.
    So, /\s+/m means, find sequences of one or more white spaces.

    gsub means replace all.

    strip is the equivalent of trim in other languages, and removes spaces from the front and end of the string.

    As, I was writing the explanation, it could be the case where you do end up with and end-line character at the end or the beginning of the string.

    To be safe

    The code could be written as:

    str.gsub(/\s+/m, ' ').gsub(/^\s+|\s+$/m, '').split(" ")
    

    So if you had:

    str = "\n     aa bbb\n    cc    dd ee\n\n"
    

    Then you’d get:

    ["aa", "bbb", "cc", "dd", "ee"]
    

    Meaning of new code:

    ^\s+ a sequence of white spaces at the beginning of the string

    \s+$ a sequence of white spaces at the end of the string

    So gsub(/^\s+|\s+$/m, '') means remove any sequence of white space at the beginning of the string and at the end of the string.

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

Sidebar

Related Questions

I am a Ruby newbie. How can I write better for this function? can
I am trying to write a ruby function that iterates through an array of
I know that I can write a Ruby case statement to check a match
I'm using Ruby, and I want to write a function that takes a delimited
In Ruby you can write a rescue at the end of an assignment to
Is there a gem in Ruby which can write to an Excel file, with
how can i write to a mysql table with ruby ? run SQL. for
Can anyone please tell me how I can write a bash shell script that
Is there a Mathematica function like inject in Ruby? For example, if I want
In Ruby, I can write contents to a file at path as simply as:

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.