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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:13:37+00:00 2026-05-15T22:13:37+00:00

for i in (0..5) if(i==0) i=4 end puts i end In the above program

  • 0
for i in (0..5)
  if(i==0)
    i=4
  end
  puts i
end

In the above program I excepted the output as – 4 5

But instead it is – 4 1 2 3 4 5

So I conclude that loop variable isnt changing. How can change it? Can anyone tell me?

Actually, In my program I need to save the current state of loop and retrive later so that on the next start program resumes from the same point where it was left.

  • 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-15T22:13:37+00:00Added an answer on May 15, 2026 at 10:13 pm

    The only way you’ll be able to modify the loop variable is to use a while loop:

    x = (2..7).to_a
    i = 0
    while (i < x.length)
      i = 4 if i == 0
      puts x[i]
      i = i + 1
    end
    

    Output:

    6
    7
    

    In a for loop, as you’ve discovered, you can modify the loop variable and that value will hold for that iteration of the loop. On the next iteration, it will retrieve the next element from the range you specified so your modified value will essentially be overwritten.

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

Sidebar

Related Questions

I have a button that puts the string: <dt></dt><dd></dd> on to the end of
module CallableDict def method_missing method puts x[#{method}] end end The above code is my
class Hello @hello = hello def display puts @hello end end h = Hello.new
The class class A private def foo puts :foo end public def bar puts
So this pattern: def foo(&block) block.call end foo lambda { puts 'hi' } Is
asdf = [[1,2,3],[11,22,33,44,55,66],[51]] def recursive(params, index) if (index==params.size) puts DONE end currentParam = params[index]
class A def a puts 'in #a' end end class B < A def
class Foo def initialize(a) puts Hello #{a} end end module Bar def initialize(b) puts
module Test def self.model_method puts this is a module method end end class A
test_module.rb module MyModule def module_func_a puts module_func_a invoked private_b end module_function :module_func_a private def

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.