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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:09:34+00:00 2026-05-22T01:09:34+00:00

The two code fragments below should print the same thing, but they don’t. ary

  • 0

The two code fragments below should print the same thing, but they don’t.

ary = %W(1 2 5 6 B 8 5 4 6 5 6 9 7 A)
indx1 = 0...ary.index("B")
indx2 = (ary.index("A") + 1)..-1
ary[indx1], ary[indx2] = ary[indx2], ary[indx1]
puts ary.inspect

ary = %W(1 2 5 6 B 8 5 4 6 5 6 9 7 A)
ary[0...ary.index("B")], ary[(ary.index("A") + 1)..-1] = ary[(ary.index("A") + 1)..-1],  ary[0...ary.index("B")]
puts ary.inspect

The first prints:

["B", "8", "5", "4", "6", "5", "6", "9", "7", "A", nil, nil, nil, nil, "1", "2", "5", "6"]

and the second:

["B", "8", "5", "4", "6", "5", "6", "9", "7", "A", "1", "2", "5", "6"]

Shouldn’t they print the same thing? They seem equivalent to me.

(Using Mac OSX 10.6.7 and ruby 1.9.2-p180)

  • 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-22T01:09:35+00:00Added an answer on May 22, 2026 at 1:09 am

    They’re different because in the first case you’re pre-calculating the indices, while in the second you’re calculating them dynamically, and the array changes between the first assignment and the second.

    Technically both RHS evaluations are made before the LHS assignments are made, but both LHS assignments can’t be made simultaneously, so in this case you’re effectively seeing that

    A, B = C, D
    

    is equivalent to

    A = C
    B = D
    

    So the first thing you do is…

    ary[0...ary.index("B")] = ary[(ary.index("A") + 1)..-1]
    

    in both cases. So ary is now

    ["B", "8", "5", "4", "6", "5", "6", "9", "7", "A"]
    

    Now originally you’d calculated indx1 and indx2 as 0...4 and 14..-1 respectively, but now if you recalculated the indx1 and indx2 values you’d have:

    indx1 = 0...ary.index("B")       #=> 0...0
    indx2 = (ary.index("A") + 1)..-1 #= 10..-1
    

    In other words,

    ary[indx2] = ary[indx1]
    

    is no longer equivalent to

    ary[(ary.index("A") + 1)..-1] = ary[0...ary.index("B")]
    

    That is,

    ary = ["B", "8", "5", "4", "6", "5", "6", "9", "7", "A"]
    ary[(ary.index("A") + 1)..-1] = ary[0...ary.index("B")]
    

    gives you

    ary #=> ["B", "8", "5", "4", "6", "5", "6", "9", "7", "A"]
    

    while

    ary = ["B", "8", "5", "4", "6", "5", "6", "9", "7", "A"]
    ary[indx2] = ary[indx1]
    

    gives you

    ary #=> ["B", "8", "5", "4", "6", "5", "6", "9", "7", "A", nil, nil, nil, nil, "1", "2", "5", "6"]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below are three fragments of code, the first two are interfaces for two objects
Below are two fragments (ready to compile) of code. In first fragment in which
Here are two chunks of code that accomplish (what I think is) the same
Which two code fragments correctly create and initialize a static array of int elements?
Consider the following two fragments of code: scala> def f1(x:Any) = x match {
When merging two code branches in Team Foundation Server I get the following error:
I have two code bases of an application. I need to copy all the
Take the following two lines of code: for (int i = 0; i <
I have these two pieces of code, wich one is more readable? foreach decimal
I've got two branches of code. 1 has a dialog box that the other

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.