Skip to content

Conversation

Bijay-Shre-stha
Copy link

Because

The lesson currently uses the term immutable incorrectly. The example:

immutable = Array.new(3) { Array.new(2) }
creates an array that can clearly be changed:
immutable[0][0] = 1000
immutable << "new element"

This might confuse students coming from other languages where “immutable” has a stricter meaning. A clearer description would help avoid misunderstanding.

This PR

  • Updates the description of the example to avoid using the word “immutable.”
  • Clarifies that the array contains references to separate objects (nested arrays) which can be independently modified.
  • Adds an explanation about the difference between using { Array.new(2) } vs Array.new(3, Array.new(2)).

Issue

Pull Request Requirements

  • I have thoroughly read and understand The Odin Project curriculum contributing guide
  • The title of this PR follows the location of change: brief description of change format, e.g. Intro to HTML and CSS lesson: Fix link text
  • The Because section summarizes the reason for this PR
  • The This PR section has a bullet point list describing the changes in this PR
  • If this PR addresses an open issue, it is linked in the Issue section
  • If any lesson files are included in this PR, they have been previewed with the Markdown preview tool to ensure it is formatted correctly
  • If any lesson files are included in this PR, they follow the Layout Style Guide

@github-actions github-actions bot added the Content: Ruby Involves the Ruby course label Oct 6, 2025
@oz123
Copy link

oz123 commented Oct 7, 2025

The explicit explanation is indeed a welcome addition. I'd still give a few more minutes on how to avoid using the word immutable in the paragraph above it.

@Bijay-Shre-stha
Copy link
Author

@oz123 so that i can make it nested_arrays? so that i can be more simpler to understand?

@oz123
Copy link

oz123 commented Oct 7, 2025

I was referring to the paragraph that starts with:

To create an immutable array of mutable objects ...

This feels very confusing for me. This example :

mutable = Array.new(3, Array.new(2))
#=> [[nil, nil], [nil, nil], [nil, nil]]
mutable[0][0] = 1000
#=> 1000
mutable
#=> [[1000, nil], [1000, nil], [1000, nil]]

I think this should not be termed "mutable" but as "caution" this syntax is not doing what you expect.

I would first show the "correct" way to create the collection:

nested = Array.new(3) { Array.new(2) }

And than add a caution block that shows what happens with the syntax:

mutable = Array.new(3, Array.new(2))

Thank you for your effort on this.

Copy link
Contributor

@JoshDevHub JoshDevHub left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good idea. Also for clarity, when you say

This might confuse students coming from other languages where “immutable” has a stricter meaning.

It also has a strict meaning in the Ruby language. The example given in the lesson is just incorrect with its use of mutable/immutable

Anyways, before I approve, I think the example given above your changes should be altered as well. The line that says:

To create an immutable array of mutable objects (string, array, hash, etc), you will need to pass the default value for Array.new via a block, using curly braces, instead of the second optional argument.

Probably needs to be something like this (omitting the incorrect 'immutable' word):

To correctly create an array of mutable objects (string, array, hash, etc), you will need to pass the default value for Array.new via a block, using curly braces, instead of the second optional argument.

The variable name in the example below that could probably be changed to nested_array as well instead of mutable.

How do these changes sound?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Content: Ruby Involves the Ruby course
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants