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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:13:45+00:00 2026-05-30T13:13:45+00:00

I was going through The Well Grounded Rubyist and got confused by the following

  • 0

I was going through The Well Grounded Rubyist and got confused by the following example.

Suppose we have an array of strings:

    numbers = ["one", "two", "three"]

If I freeze this array, I can’t do the following:

    numbers[2] = "four"

That statement is a Runtime error, but this:

    numbers[2].replace("four")

is not.

The book explains that in the first of the last two statements, we are trying to access the array. That’s what I found confusing because I thought we are trying to access the third element of the array, which is a string object. And how is that different from the last statement?

  • 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-30T13:13:46+00:00Added an answer on May 30, 2026 at 1:13 pm

    It’s different because in the statement that works you are calling String#replace. As you might expect, a call to Array#replace will fail.

    numbers.replace [1,2,3]
    TypeError: can't modify frozen array
    

    The object reference at any given array index might be arbitrarily complicated and it’s not the job of the frozen array to keep those objects from changing … it just wants to keep the array from changing. You can see this:

    ree-1.8.7> numbers[2].object_id
     => 2149301040 
    ree-1.8.7> numbers[2].replace "four"
     => "four" 
    ree-1.8.7> numbers[2].object_id
     => 2149301040
    

    numbers[2] has the same object_id after String#replace runs; the Array did not actually change.

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

Sidebar

Related Questions

Going through Javascript documentation, I found the following two functions on a Javascript object
Going through the microsoft authentication tutorial listed here they have you create a master
While going through university and from following the development of SO, I've heard a
Well, I've been going through my personal hell these days I am having some
I was going through one of the threads. A program crashed because it had
Well, I was going through an application where a find a variable named as
I'm going through a tough decision (at least for me)... My website would have
I have a while loop that is going through and displaying an RSS icon
I was going through operator precedence section of php.net and came across this example
I'm going through the Spring MVC tutorial. I have a build.xml and build.properties file

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.