How are ruby arrays internally implemented (mainly in CRuby, but any other info is welcomed)?
Are they growable arrays like a c++ vector or are they list based? What’s the complexity of shift/unshift and accessing an element by index?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
They’re growable arrays which “grow at the end”.
shiftisO(1),unshiftisO(n)and accessing by index isO(1). To the best of my knowledge this holds true for all ruby implementations, but it definitely does in MRI.UPDATE: After this answer was originally written, Ruby was enhanced to make
unshiftamortizedO(1). The enhanced array is in Ruby 2.0.0 and later, makingshift,unshift,push, andpopallO(1)or amortizedO(1).