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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:16:29+00:00 2026-05-24T07:16:29+00:00

A friend asked me the Ruby best and performant way to achieve the effect

  • 0

A friend asked me the Ruby best and performant way to achieve the effect of JavaScript’s splice method in Ruby.
This means no iteration on the Array itself or copies.

“begin at index start, remove length items and (optionally) insert elements. Finally return the removed items in an array.” << This is misleading, see the JS example below.

http://www.mennovanslooten.nl/blog/post/41

Quick hack that doesn’t have the optional substitution:

from_index     = 2
for_elements   = 2
sostitute_with = :test
initial_array  = [:a, :c, :h, :g, :t, :m]
# expected result: [:a, :c, :test, :t, :m]
initial_array[0..from_index-1] + [sostitute_with] + initial_array[from_index + for_elements..-1]

What’s yours?
One line is better.

Update:

// JavaScript
var a = ['a', 'c', 'h', 'g', 't', 'm'];
var b = a.splice(2, 2, 'test'); 
> b is now ["h", "g"]
> a is now ["a", "c", "test", "t", "m"]

I need the resulting ‘a’ Array, not ‘b’.

  • 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-24T07:16:30+00:00Added an answer on May 24, 2026 at 7:16 am

    Use Array#[]=.

    a = [1, 2, 3, 4, 5, 6]
    a[2..4] = [:foo, :bar, :baz, :wibble]
    a # => [1, 2, :foo, :bar, :baz, :wibble, 6]
    
    # It also supports start/length instead of a range:
    a[0, 3] = [:a, :b]
    a # => [:a, :b, :bar, :baz, :wibble, 6]
    

    As for returning the removed elements, []= doesn’t do that… You could write your own helper method to do it:

    class Array
      def splice(start, len, *replace)
        ret = self[start, len]
        self[start, len] = replace
        ret
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A friend asked me this and I was stumped: Is there a way to
I friend asked me this today. Picasa Web has a cool (and frightening :-)
A friend asked me yesterday if this was possible on the iPhone. I took
My friend asked this question I am not sure why it is on my
This could be a little off the ballpark, but a friend asked me about
My friend asked me this question today. How to test a vending machine and
A friend has asked me this, and I couldn't answer. He asked: I am
this is an interview question my friend was asked yesterday. The question was something
Yesterday my friend asked me a question about this query: select * from user
This is just a random question my friend asked me which I also do

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.