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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:18:07+00:00 2026-06-14T12:18:07+00:00

I got confused about the iteration in ruby.In the following code I wrote, I

  • 0

I got confused about the iteration in ruby.In the following code I wrote, I expected that the two paths print out should be the same. But actually they are not. Seems the path was changed in the for loop.

Anything wrong in my code? Thanks

def one_step_search(dest,paths)
  direction = %w(north east south west)
  new_paths = []
  paths.map do |path|

    print "original path is: "
    print_path path

    curr_room = path.last
    for i in 0..3
      new_path = path
      if !curr_room.send("exit_#{direction[i]}").nil?
        next_room_tag = curr_room.send("exit_#{direction[i]}")[0] 
        next_room = find_room_by_tag(next_room_tag)
        if !new_path.include?(next_room)  # don't go back to the room visited before
          new_path << next_room
          new_paths << new_path  
          print "new path is: "
          print_path path
          return new_paths if dest.tag == next_room_tag
        end
      end
    end
  end

  return new_paths
end
  • 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-14T12:18:08+00:00Added an answer on June 14, 2026 at 12:18 pm

    It seems to me that problem is in this line

    new_path = path
    

    You may think that new_path and path are different objects but it’s not. I’ll illustrate by example:

    a = "foo"
    b = a
    puts a.sub!(/f/, '_')
    puts a                   # => "_oo"
    puts b                   # => "_oo"
    

    a and b are references that pointing to one object.
    The simpliest solution for you will be to use dup or clone

    new_path = path.clone
    

    but actually your code requires good cleaning.

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

Sidebar

Related Questions

I have the following code and got myself confused: I have a query that
I've got a problem that I'm rather confused about. I have the following lines
I don't use C that much and I recently got confused about 2d array
I'm confused about an aspect of polymorphism. Please consider the following code: #include <iostream>
I've got confused . We hear a lot about Provider in ASP.NET . Membership-Provider
I'm relatively confused about this... I've got a table like: +----------------+--------------------------------------------------+------+-----+-------------------+-----------------------------+ | Field |
Today i was reading about pure function, got confused with its use: A function
I got completely confused with gae. I have a script, that does a post
I'm writing my first location based android app, but got confused about some of
OK, I'm confused about sql server indexed views(using 2008) I've got an indexed view

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.