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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:59:06+00:00 2026-05-27T06:59:06+00:00

Say I have an array: array = [6, 6, 6, 4, 4, 6, 4,

  • 0

Say I have an array:

array = [6, 6, 6, 4, 4, 6, 4, 4]

and I have another array of strings:

quadrant = ["upper_left", "upper_right", "lower_left", "lower_right"]

and I have a 8 x 8 2-d array consisting of board locations(@board) that are either nil or Checker objects.

My goal is to create a hash such:

hash = { "upper_left" => @board[array[0]][array[1]] ,
         "upper_right" => @board[array[2]][array[3]] ,
         "lower_left" => @board[array[4]][array[5]] ,
         "lower_left" => @board[array[6]][array[7]] }

I have the following code:

jump_positions = {}
QUADRANTS.each do |quad|
        array.each_slice(2) do |coord|
          jump_positions[quad] = @board[coord[0]][coord[1]]
        end

And then the test:

it "should assign board locations as adjacent positions and deliver that info as a whole" do
    @bs.board = board.create_board
    x_coord = 3
    y_coord = 1
    jump_assignments = @bs.assign_adjacent_positions(x_coord, y_coord)
    jump_assignments["upper_left"].class.should == nil
    jump_assignments["upper_right"].class.should == nil
    jump_assignments["lower_left"].class.should == Checker
    jump_assignments["lower_right"].class.should == Checker 
end

fails because all the assignments are of class ‘Checker’ and turns out they’re all the same ‘Checker’ object. I know its doing this because the loops are nested so all the ‘quad’ keys are getting initialize to the last board location.

Is there a way I can assign the value to the key with a value from the ‘array’ in one pass so that they get assigned correctly? Does this question even make sense?

  • 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-27T06:59:06+00:00Added an answer on May 27, 2026 at 6:59 am

    I think you just need to add a little map to my answer to your other similar question:

    hash = Hash[quadrant.zip(array.each_slice(2).map { |a| @board[a.first][a.last] })]
    

    Given a board like this:

    @board = [
        ['11', '12', ... '18'],
        ['21', '22', ... '28'],
        ...
        ['81', '82', ... '88']
    ]
    

    the above construct gives me a hash like this:

    {
        "upper_left"  => "77",
        "upper_right" => "75",
        "lower_left"  => "57",
        "lower_right" => "55"
    }
    

    and that seems to be what you’re looking for.

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

Sidebar

Related Questions

I have an array tab1 that contains some strings and another array that could
Lets say I have an array : a=[[1,2,3],[4,5]] and I have another array :
Say I have an array that represents a set of points: x = [2,
Say I have an array of strings in a php array called $foo with
Say I have an array of strings: string[] strArray = {aa, bb, xx, cc,
Say we have a struct that it's data is provided by un-managed byte array
Say I have the following array of strings: $data_array = array(the dog is hairy,
Say I have an array of [34, 35, 45, 48, 49] and another array
I have a 2d array say, Array A[60][150] and have another array, Array B[60][150].
Basically, I have an array, let's say @badvalues . I have another array, let's

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.