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

  • Home
  • SEARCH
  • 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 4001686
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:56:07+00:00 2026-05-20T07:56:07+00:00

arr = [red,green,blue,yellow] arr.each do |colour| if colour == red colour = green end

  • 0
arr = ["red","green","blue","yellow"]

arr.each do |colour|
  if colour == "red"
    colour = "green"
  end
end

puts arr.inspect

The above code outputs:

["red", "green", "blue", "yellow"]

but why not?

["green", "green", "blue", "yellow"]

I thought that colour was a reference to the current element in the array, and whatever i did to it would effect that array element?

  • 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-20T07:56:08+00:00Added an answer on May 20, 2026 at 7:56 am

    When you’re inside the arr.each block, the colour variable is bound to one of the objects in the arr array.

    However, as soon as you make the assignment colour = "green" in the block, now the colour variable is bound to a new object (namely a String with a value of "green"), and the original arr remains unaffected.

    One way to achieve what you’re talking about would be:

    arr.each_index do |i|
      arr[i] = "green" if arr[i] == "red"
    end
    

    which manipulates the array directly.

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

Sidebar

Related Questions

arr = [red,green,yellow] arr2 = arr.clone arr2[0].replace(blue) puts arr.inspect puts arr2.inspect produces: [blue, green,
Imgagining i have two array arr[]={red,blue,green} arr2[]={red,violet,black} How will i compare the two array
Dim arr As New Collection, a Dim aFirstArray() As Variant ...some code For Each
how could I force $.each(arr, callback(key,value){...}) to stop when I found what I was
char arr[100]; cin.get(arr,100); Is this safe? Will the null-character be appended at the end
Given this code: var arr = []; for (var i = 0; i <
Given an Array arr of size 100000, each element 0 <= arr[i] < 100
function calcTotalScore(){ var arr = [A,B,C,D,E,F]; $.each(arr, function(n,val){ calcTotal(val); }); } calcTotal(value){ // sample
I have an array like this arr = [orange,red,black,white] I want to augment the
var arr = []; $('#menu').children().each(function(){ arr = $(this).width(); }); console.log(arr); It doesn't work that

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.