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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T14:12:14+00:00 2026-05-23T14:12:14+00:00

In Ternary operator , a person wanting to join [foo, bar, baz] with commas

  • 0

In Ternary operator, a person wanting to join ["foo", "bar", "baz"] with commas and an “and” cited The Ruby Cookbook as saying

If efficiency is important to you,
don’t build a new string when you can
append items onto an existing string.
[And so on]… Use str << var1 << ‘ ‘
<< var2 instead.

But the book was written in 2006.

Is using appending (ie <<) still the fastest way to build a large string given an array of smaller strings, in all major implementations of Ruby?

  • 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-23T14:12:14+00:00Added an answer on May 23, 2026 at 2:12 pm

    Use Array#join when you can, and String#<< when you can’t.

    The problem with using String#+ is that it must create an intermediary (unwanted) string object, while String#<< mutates the original string. Here are the time results (in seconds) of joining 1,000 strings with ", " 1,000 times, via Array#join, String#+, and String#<<:

    Ruby 1.9.2p180      user     system      total        real
    Array#join      0.320000   0.000000   0.320000 (  0.330224)
    String#+ 1      7.730000   0.200000   7.930000 (  8.373900)
    String#+ 2      4.670000   0.600000   5.270000 (  5.546633)
    String#<< 1     1.260000   0.010000   1.270000 (  1.315991)
    String#<< 2     1.600000   0.020000   1.620000 (  1.793415)
    
    JRuby 1.6.1         user     system      total        real
    Array#join      0.185000   0.000000   0.185000 (  0.185000)
    String#+ 1      9.118000   0.000000   9.118000 (  9.118000)
    String#+ 2      4.544000   0.000000   4.544000 (  4.544000)
    String#<< 1     0.865000   0.000000   0.865000 (  0.866000)
    String#<< 2     0.852000   0.000000   0.852000 (  0.852000)
    
    Ruby 1.8.7p334      user     system      total        real
    Array#join      0.290000   0.010000   0.300000 (  0.305367)
    String#+ 1      7.620000   0.060000   7.680000 (  7.682265)
    String#+ 2      4.820000   0.130000   4.950000 (  4.957258)
    String#<< 1     1.290000   0.010000   1.300000 (  1.304764)
    String#<< 2     1.350000   0.010000   1.360000 (  1.347226)
    
    Rubinius (head)     user     system      total        real
    Array#join      0.864054   0.008001   0.872055 (  0.870757)
    String#+ 1      9.636602   0.076005   9.712607 (  9.714820)
    String#+ 2      6.456403   0.064004   6.520407 (  6.521633)
    String#<< 1     2.196138   0.016001   2.212139 (  2.212564)
    String#<< 2     2.176136   0.012001   2.188137 (  2.186298)
    

    Here’s the benchmarking code:

    WORDS = (1..1000).map{ rand(10000).to_s }
    N = 1000
    
    require 'benchmark'
    Benchmark.bmbm do |x|
      x.report("Array#join"){
        N.times{ s = WORDS.join(', ') }
      }
      x.report("String#+ 1"){
        N.times{
          s = WORDS.first
          WORDS[1..-1].each{ |w| s += ", "; s += w }
        }
      }
      x.report("String#+ 2"){
        N.times{
          s = WORDS.first
          WORDS[1..-1].each{ |w| s += ", " + w }
        }
      }
      x.report("String#<< 1"){
        N.times{
          s = WORDS.first.dup
          WORDS[1..-1].each{ |w| s << ", "; s << w }
        }
      }
      x.report("String#<< 2"){
        N.times{
          s = WORDS.first.dup
          WORDS[1..-1].each{ |w| s << ", " << w }
        }
      }
    end
    

    Results obtained on Ubuntu under RVM. Results from Ruby 1.9.2p180 from RubyInstaller on Windows are similar to the 1.9.2 shown above.

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

Sidebar

Related Questions

Which ternary operator in C# is most popular and mostly used?
Please demonstrate how the ternary operator works with a regular if/else block. Example: Boolean
I wanna make use of the Ternary Operator with an object. if($msg == 'hello'){
Possible Duplicates: Nullable types and the ternary operator. Why won’t this work? Conditional operator
Which method is faster or more responsive in javascript, if-else, the ternary operator or
Is there a ternary conditional operator in Python?
Possible Duplicate: Python Ternary Operator In some languages including Java, C/C++, C#, etc. you
How to use ternary operator with System::Boolean? This sample code always returns true: bool
With the ternary operator, it is possible to do something like the following (assuming
Possible Duplicate: Python Ternary Operator If Python would support the (x ? a :

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.