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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:41:15+00:00 2026-06-05T16:41:15+00:00

I’m still learning basics of ruby so I thought I’d attempt to make a

  • 0

I’m still learning basics of ruby so I thought I’d attempt to make a multiplication table

x = (0..7).to_a
y = []
x.each { y << x }

y is now an 8×8 array where each row is equal. These are just placeholder values that I don’t want, so I attempt to iterate through the array indices to change the values.

x.each { |i| y[i][i] = i * i }

At this point, y becomes an array containing 8 columns of [0, 1, 4, 9, 16, ...]. I understand why I’m only receiving squared values, but I don’t understand why y[1][3], for example, is altered at all, when I’d only expect values at indices such as [1][1], [2][2] to be altered.

And furthermore, the value of x is reassigned to [0, 1, 4, 9, 16, ...] as well, even though I never intentionally reassigned its value. By the above portion of code, its value is reassigned automatically, and I cannot understand why.

I could always look up another method for making such a table myself, but I want to understand what’s happening to x that causes it to change its value and why iteration through the indices doesn’t behave as expected. I’ll move on to iterating more properly over each index of the 2D array when I can grok why my simpler attempt is producing an invalid output. I’d like to figure out this rather basic task on my own, but I’ve hit a roadblock for now.

  • 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-05T16:41:17+00:00Added an answer on June 5, 2026 at 4:41 pm

    What you’ve put in y is 8 references to the exact same object x. So when you alter y[1][1], that is the same thing as x[1], which is the same thing as y[2][1] and y[3][1] and so on.

    The value of x isn’t getting reassigned. Rather, x is an 8-element array; this object is an element of y eight times as well as being the value of x; each iteration of the each is modifying the contents of that array.

    This is pretty counterintuitive if you’re not used to it. Perhaps the following gruesome analogy will help. Suppose you have just started up a secret society, and at the moment you are the only member. (It’s a very secret society.) In particular: You are the President of the society, and its Secretary, and its Treasurer. You also have a job; you are, let’s say, the CEO of Microsoft.

    Now, a member of a rival secret society comes and breaks the left toe of the President of your secret society. (Apparently it wasn’t quite secret enough.) Another comes and breaks the right kneecap of the Secretary. Another smashes out the teeth of the Treasurer. And now, lo, it turns out that the CEO of Microsoft has suffered all these indignities. How can that be? No one ever told anyone to break the kneecaps of the CEO of Microsoft!

    Of course there’s nothing difficult about this: the very same person can fill all those roles; you get the exact same person if you ask for the President of the Mangrove-Throat-Wobbler Society as if you ask for its Secretary, or if you ask for the CEO of Microsoft.

    In Ruby (and many other languages like it), the relationship between a variable and its value, or a position in an array and the thing held there, is much more like the relationship between an official post and its holder than, e.g., like the relationship between a box and its contents. The exact same thing can be the value of many variables, just as the exact same person can be President and Secretary and Treasurer and Grand Pooh-Bah.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have two tables with like below codes: Table: Accounts id | username |
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.