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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:17:59+00:00 2026-06-12T00:17:59+00:00

Just wondering why def move world_switch(@pos_X += 1, @pos_X -= 1, @pos_Y += 1,

  • 0

Just wondering why

def move
  world_switch(@pos_X += 1, @pos_X -= 1, @pos_Y += 1, @pos_Y -= 1)
end

  def world_switch(do_on_north, do_on_south, do_on_east, do_on_west)
    case @facing # => 'NORTH'
    when 'NORTH'
      puts do_on_north # => 1
      do_on_north
    when 'SOUTH'
      do_on_south
    when 'EAST'
      do_on_east
    when 'WEST'
      do_on_west
    end
  end

Calling world_switch:

robot = Robot.new(0, 0, 'NORTH')
robot.move
puts robot.instance_variable_get("@pos_X") #=> 0

results in changing nothing, I would like to increase or decrease instance variable @pos_X or @pos_Y

This is my initialize method

def initialize(pos_X, pos_Y, facing)
    @pos_X, @pos_Y, @facing = pos_X, pos_Y, facing
  end

and that’s how I create an instance of the class robot = Robot.new(0, 0, 'NORTH')

All help will be appreciated

  • 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-12T00:18:01+00:00Added an answer on June 12, 2026 at 12:18 am

    The explanation for the current behaviour is as Chowlett described, but did you intend for your @pos_X += 1, @pos_X -= 1 etc in move to be blocks of code and then for exactly one of these to be called from world_switch depending on which way the robot is facing?

    If so, move needs to be declared like this

    def move
      world_switch(Proc.new { @pos_X += 1 }, Proc.new { @pos_X -= 1 },
        Proc.new { @pos_Y += 1 }, Proc.new { @pos_Y -= 1 })
    end
    

    and then in world_switch you can do something like

    case @facing # => 'NORTH'
        when 'NORTH'
          do_on_north.call
        when 'SOUTH'
          do_on_south.call
     ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

just wondering if there is a way to reduce the amount of code needed
Just wondering: I'm trying to set up an adaptive image handler in Coldfusion8, which
Just wondering the best way to replace in place matches on a string. value.replace(bob,
Just wondering, having the following simple code: var object1 = { name: function (){
Just wondering when do you actually use $.get(index); I am a bit confused on
Just wondering for user permission check, Should you keep the permission in the session
just wondering when I had an iphone I was able to create a web
Just wondering is it possible to build CLI app that can be run from
Just wondering what the best way to access read only information form a Microsoft
Just wondering if anyone can optimize this usortMonths() function to be better? Basically I

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.