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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T20:44:15+00:00 2026-05-13T20:44:15+00:00

ruby: What is the most optimized expression to evaluate the same as result as

  • 0

ruby: What is the most optimized expression to evaluate the same as result as with

phrase.split(delimiter).collect {|p| p.lstrip.rstrip }
  • 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-13T20:44:15+00:00Added an answer on May 13, 2026 at 8:44 pm

    Optimised for clarity I would prefer the following:

    phrase.split(delimiter).collect(&:strip)
    

    But I presume you want to optimise for speed. I don’t know why others are speculating. The only way to find out what is faster is to benchmark your code.

    Make sure you adjust the benchmark parameters – this is just an example.

    require "benchmark"
    
    # Adjust parameters below for your typical use case.
    n = 10_000
    input = " This is - an example. - A relatively long string " +
      "- delimited by dashes. - Adjust if necessary " * 100
    delimiter = "-"
    
    Benchmark.bmbm do |bench|
      bench.report "collect { |s| s.lstrip.rstrip }" do
        # Your example.
        n.times { input.split(delimiter).collect { |s| s.lstrip.rstrip } }
      end
    
      bench.report "collect { |s| s.strip }" do
        # Use .strip instead of .lstrip.rstrip.
        n.times { input.split(delimiter).collect { |s| s.strip } }
      end
    
      bench.report "collect { |s| s.strip! }" do
        # Use .strip! to modifiy strings in-place.
        n.times { input.split(delimiter).collect { |s| s.strip! } }
      end
    
      bench.report "collect(&:strip!)" do
        # Slow block creation (&:strip! syntax).
        n.times { input.split(delimiter).collect(&:strip!) }
      end
    
      bench.report "split(/\\s*\#{delim}\\s*/) (static)" do
        # Use static regex -- only possible if delimiter doesn't change.
        re = Regexp.new("\s*#{delimiter}\s*")
        n.times { input.split(re) }
      end
    
      bench.report "split(/\\s*\#{delim}\\s*/) (dynamic)" do
        # Use dynamic regex, slower to create every time?
        n.times { input.split(Regexp.new("\s*#{delimiter}\s*")) }
      end
    end
    

    Results on my laptop with the parameters listed above:

                                          user     system      total        real
    collect { |s| s.lstrip.rstrip }   7.970000   0.050000   8.020000 (  8.246598)
    collect { |s| s.strip }           6.350000   0.050000   6.400000 (  6.837892)
    collect { |s| s.strip! }          5.110000   0.020000   5.130000 (  5.148050)
    collect(&:strip!)                 5.700000   0.030000   5.730000 (  6.010845)
    split(/\s*#{delim}\s*/) (static)  6.890000   0.030000   6.920000 (  7.071058)
    split(/\s*#{delim}\s*/) (dynamic) 6.900000   0.020000   6.920000 (  6.983142)
    

    From the above I might conclude:

    • Using strip instead of .lstrip.rstrip is faster.
    • Preferring &:strip! over { |s| s.strip! } comes with a performance cost.
    • Simple regex patterns are nearly as fast as using split + strip.

    Things that I can think of that may influence the result:

    • The length of the delimiter (and whether or not it is whitespace).
    • The length of the strings that you want to split.
    • The length of the splittable chunks in the string.

    But don’t take my word for it. Measure it!

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

Sidebar

Ask A Question

Stats

  • Questions 313k
  • Answers 313k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Unfortunately, the only way I could fix this was to… May 13, 2026 at 10:48 pm
  • Editorial Team
    Editorial Team added an answer How do I explain to the admin that primaryName needs… May 13, 2026 at 10:48 pm
  • Editorial Team
    Editorial Team added an answer Sorpigal is right... but I suggest parseInt instead of int...… May 13, 2026 at 10:48 pm

Related Questions

In Ruby, what is the most expressive way to map an array in such
For scripting languages, what is the most effective way to utilize a console when
What is the clearest way to comma-delimit a list in Java? I know several
With the many testing framework that is available in the Ruby community namely: unittest,
I'm creating a ruby server which is connecting to a TCP client. My server

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.