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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T04:57:21+00:00 2026-06-02T04:57:21+00:00

I know that shift, push, and pop are Array methods used to add/remove elements

  • 0

I know that shift, push, and pop are Array methods used to add/remove elements of an array, but I am unsure of what is actually occuring in memory. Say for example the method pop which removes the last element of an array. It reminds me of the LIFO order used in the stack, but I assume the element isn’t really “popped” like in assembly programming; rather that the index of the entire array is shifted. I really don’t know so if anyone could help me out I would really appreciate it.

  • 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-02T04:57:23+00:00Added an answer on June 2, 2026 at 4:57 am

    Ruby is for programmers who suffers from the idea that you have. We trust those programmers who do the implementation that they can do best in optimizing performance and memory management.

    If you are just curious, here is the code for Array#shift in Rubinius:

    def shift(n=undefined)
        Rubinius.check_frozen
    
        if n.equal? undefined
          return nil if @total == 0
          obj = @tuple.at @start
          @tuple.put @start, nil
          @start += 1
          @total -= 1
    
          obj
        else
          n = Rubinius::Type.coerce_to(n, Fixnum, :to_int)
          raise ArgumentError, "negative array size" if n < 0
    
          slice!(0, n)
        end
    end
    

    And you can see, an array itself is a Rubinius::Tuple, and in Tuple definition, it is an Rubinius::Array. What it does is just put the beginning position to the next one. I am not sure they will release the space it used (which I assume it will) because you have to dig deeper.

    In official 1.9.3, I do not know how it is implemented as they do it in C, and they are hard to read. If you want to know more details, you can fork Rubinius on GitHub, or fork the official 1.9.3 from ruby-lang.org, and read the source code. You can learn more about C/C++ programming too 🙂


    So I quickly went through the code of official 1.9.3, and this is the array#shift function definition:

    static VALUE
    rb_ary_shift_m(int argc, VALUE *argv, VALUE ary)
    {
        VALUE result;
        long n;
    
        if (argc == 0) {
        return rb_ary_shift(ary);
        }
    
        rb_ary_modify_check(ary);
        result = ary_take_first_or_last(argc, argv, ary, ARY_TAKE_FIRST);
        n = RARRAY_LEN(result);
        if (ARY_SHARED_P(ary)) {
        if (ARY_SHARED_NUM(ARY_SHARED(ary)) == 1) {
            rb_mem_clear(RARRAY_PTR(ary), n);
        }
            ARY_INCREASE_PTR(ary, n);
        }
        else {
        MEMMOVE(RARRAY_PTR(ary), RARRAY_PTR(ary)+n, VALUE, RARRAY_LEN(ary)-n);
        }
        ARY_INCREASE_LEN(ary, -n);
    
        return result;
    }
    

    This line:

    MEMMOVE(RARRAY_PTR(ary), RARRAY_PTR(ary)+n, VALUE, RARRAY_LEN(ary)-n);
    

    It tells us it actually move the memory block offset by n. That is probably why the official runs slower than Rubinius… Rubinius takes advantage in large memory, but save time; official consumes less memory but takes more time…

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

Sidebar

Related Questions

I know that Ctrl + Shift + B launches a solution build, but I
I know that F7/Shift F7 will switch between Code behind and ASPX pages, but
I'm having a problem understanding the shift/reduce confict for a grammar that I know
I know that lot of questions about HTML sanitizers have appeared in SO, but
I know that all of these will be compiled together into one file but
I know that similar question was asked several times, but still I can't make
I know that you can do ctrl-shift-g to find all calls to a particular
I know that I can perform divide by 2 using right shift. For simplicity,
I know that VB offers many things to control it : SDK, API, COM,
i know that it may be difficult to see the actual problem without looking

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.