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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:49:09+00:00 2026-05-20T02:49:09+00:00

The Short and Sweet Running this code in Ruby 1.9: FOO = global constant

  • 0

The Short and Sweet

Running this code in Ruby 1.9:

FOO = "global constant"

class Something
  FOO = "success!"

  def self.handle &block
    self.new.instance_eval &block
  end
end

class Other
  FOO = "wrong constant"

  def self.handle
    Something.handle{FOO}
  end
end

puts Something.handle{FOO}
puts Other.handle

I get “success!” and “wrong constant”. How can I get both calls to print “success!”? This is not a contrived exercise for fun – I wouldn’t waste people’s time for that. I have a real-world problem, and I’ve whittled it down to the simplest possible example that demonstrates the issue. Read on for the “why”.

A More Thorough Explanation

Calling Something.handle{FOO} works correctly. Ruby uses the definition of FOO given in the Something class. However, If I try to call it the same way from another class, it gives me the definition of FOO for that class.

I thought the idea of tighter constant looksups in Ruby 1.9 was to avoid problems like this. instance_eval is supposed to use the scope of its receiver (self.new, in this case), not the scope of the calling block. It works for things like instance variables, but not for constants. This is not a problem of precedence – remove the “global” and “wrong” constants, and ruby still won’t be able to find the remaining correct constant.

The real-world problem I’m having is that I have a module with several classes. I have a method that accepts a block, and runs the block in the context of the module. Inside that block, I want to be able to refer to those classes by their short names (like I can anywhere in the module itself), rather than having to prepend the module name.

This is painful:

ThirdPartyApis::MyAnswerSite.connection question.id, answer.id do |question_id, answer_id|
  question = ThirdPartyApis::MyAnswerSite::Question.find question_id
  answer = ThirdPartyApis::MyAnswerSite::Answer.find answer_id

  ThirdPartyApis::MyAnswerSite::Solution.new question, answer
end

This is pleasant:

ThirdPartyApis::MyAnswerSite.connection question.id, answer.id do |question_id, answer_id|
  question = Question.find question_id
  answer = Answer.find answer_id

  Solution.new question, answer
end

Summary

So that’s the long-winded explanation. Please don’t offer workarounds that don’t address my question. I appreciate the desire to explore other avenues, but my question is simple: can ONLY the Something class by modified, in a way that prints “success!” twice at the end? That’s the test case, and any solution that fits this is my accepted answer, and its author is my personal hero for the week. Please!

  • 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-20T02:49:10+00:00Added an answer on May 20, 2026 at 2:49 am

    I got it to work using the sourcify gem (gem install sourcify):

    require 'sourcify'
    FOO = "global constant"
    
    class Something
      FOO = "success!"
    
      def self.handle &block
        (self.new.instance_eval(block.to_source)).call
      end
    
    end
    
    class Other
      FOO = "wrong constant"
    
      def self.handle
        Something.handle{FOO}
      end
    end
    
    puts Something.handle{FOO}
    => success!
    puts Other.handle
    => success!
    

    edit: Oops, you could see where I was working on using bindings too, removed that code.

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

Sidebar

Related Questions

Short and sweet, Is it ok to use (current version) Ruby 1.9.1 with Rails
Short and sweet: Why does the following block of Objective-C code not produce a
Short of rolling your own. There has to be something out there. FlexLM/FlexNet is
I want to be able to run unstrusted ruby code. I want to be
I'll keep it short and sweet - I'm building an application just for practice
Short and sweet version: On one machine out of around a hundred test machines
Short and sweet: How can I tell Interface Builder to center a window on
Short and sweet version: Is there a single web service method that would return
Short and sweet question: I've inherited a Pylons site with a bunch of models.
Basically, my question is short and sweet: Is the following a bad idea (encapsulating

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.