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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:08:16+00:00 2026-06-14T17:08:16+00:00

Here’s a question to bamboozle your noodle. It seems there’s a difference between method

  • 0

Here’s a question to bamboozle your noodle.

It seems there’s a difference between method declaration and constant declaration in a block that is evaluated using class_eval with a class as the receiver. Here’s some code that demonstrates the discrepancy:

module X
  def save_block(&block)
    @block = block
  end
end

module Y
  extend X
  save_block do
    SOME_CONSTANT = 1
    def meth
      "a method"
    end
  end
  def self.included(m)
    m.class_eval &@block
  end
end

class Z
  include Y
end

class W
  include Y
end

Executing this code, at least in Ruby 1.9.3, results in the following error:

warning: already initialized constant SOME_CONSTANT

Which I didn’t expect. Consider first where meth is located:

Z.instance_methods(false)
=> [:meth]

W.instance_methods(false)
=> [:meth]

And Y doesn’t have any instance methods:

Y.instance_methods(false)
=> []

This makes sense because the block is executed with Z and W as the receivers of class_eval. However, the constants are different, hence the error message, as shown here:

Y.constants(false)
=> [:SOME_CONSTANT]

Z.constants(false) 
=> []

W.constants(false)
=> []

Here, the constant ends up getting defined in Y (for some bizarre reason) and thus when the block is executed a second time, the constant, SOME_CONSTANT is already defined.

I very much look forward to some enlightenment.

Update (2012/11/19):

As @phoet pointed out below (the the response to my comment to his answer), constants are defined in the lexical scope of the block, that is to say, in the scope where the block is originally defined. In my example, this would be Y.

  • 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-14T17:08:17+00:00Added an answer on June 14, 2026 at 5:08 pm

    as described in this question Accessing a constant constant handling is within it’s “lexical scope”, meaning the context they are defined, not the context they are executed.

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

Sidebar

Related Questions

Here's what I'm trying to accomplish with this program: a recursive method that checks
Here is the problem that I am trying to solve. I have two folders
I know there's a lot of other questions out there that deal with this
Here are some facts about my app followed by a question My app has
Here is my scenario. I have a website running under AppPool1 and that works
Here's the code I have. It works. The only problem is that the first
Here's a basic regex technique that I've never managed to remember. Let's say I'm
Here is the issue I am having: I have a large query that needs
Here's my scenario - I have an SSIS job that depends on another prior
Here's a coding problem for those that like this kind of thing. Let's see

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.