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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:11:38+00:00 2026-05-24T06:11:38+00:00

In this question I found an interesting detail about scope of a final variable

  • 0

In this question I found an interesting detail about scope of a final variable in Java. I don’t know Java good enough, but I think that final is identical to a constant in Ruby.

In C++ this is possible:

for(int i = 0; i < 5; ++i){
    const int c = i * 5;
    std::cout << c << std::endl;
}

Trying to change the value during the loop is not possible though and gives you a compile time error.

I was curious to see how Ruby would handle this, started irb and wrote this code to test it:

5.times do |x|
  XPI = x * Math::PI
  puts x
end

the result was

0.0
(irb):27: warning: already initialized constant XPI
3.141592653589793
(irb):27: warning: already initialized constant XPI
6.283185307179586
(irb):27: warning: already initialized constant XPI
9.42477796076938
(irb):27: warning: already initialized constant XPI
12.566370614359172
=> 5

So my question: Is there a way to assign a constant at the beginning of a loop that gets initialized for each loop iteration without creating warning messages? It could have some real world use cases, when I want to make a calculation based on the iterator variable and then make sure, the result does not get changed for the remaining loop.

Nothing you need every single day, but I’m just curious.

  • 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-24T06:11:39+00:00Added an answer on May 24, 2026 at 6:11 am

    There seems to be no strict equivalent to Java’s final in ruby. However, you could use remove_const (which is a private method in Module) to get rid of the constant (and the warnings) at the end of the loop:

    5.times do |x| 
      XPI = x * Math::PI; 
      puts x; 
      Object.instance_eval{ remove_const :XPI };
      # this would work, too: Object.send(:remove_const, :XPI); 
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this interesting question about converting numbers into words: Code Golf: Number to
In this question Alan posted links to interesting pages about PowerShell. I found also
I searched and found this question but did not like the answer. Is there
I found this question that is discussing what I would like to do, but
I found this question but it was only similar and, more importantly, dated by
this is a hard question. I've found nothing interesting over the web. I'm developing
I found this interesting blog post via CodingHorror: My Favorite Interview Question . In
I found this question very interesting : Programmatic Bot Detection I have a very
( This question about refactoring F# code got me one down vote, but also
This is an interview question that I found interesting. Write a method that takes

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.