Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Inconsistency in "constantize"
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Aaron Patterson  
View profile  
 More options Nov 6 2012, 6:37 am
From: Aaron Patterson <tenderl...@ruby-lang.org>
Date: Tue, 6 Nov 2012 03:37:37 -0800
Local: Tues, Nov 6 2012 6:37 am
Subject: Inconsistency in "constantize"
Hi,

I've changed `const_get` in Ruby 2.0.  If it were implemented in 1.9, it
would look roughly like this:

  def const_get name, inherited = true
    name.to_s.split('::').inject(self.class) do |klass, name|
      klass.const_get name, inherited
    end
  end

I was hoping we could use this to replace the implementation of
`constantize`, but it seems we have an inconsistency in the way that
`constantize` works in regard to constant inheritance.

I've written a test to demonstrate the problem:

  https://gist.github.com/4024158

For some reason `constantize` inherits constants, but not when the
constant is defined on Object, where `const_get` in Ruby 2.0 will always
honor the inheritance (even if the constant is defined at the top).

Is there any chance we can change this?

--
Aaron Patterson
http://tenderlovemaking.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Xavier Noria  
View profile  
 More options Nov 6 2012, 7:37 am
From: Xavier Noria <f...@hashref.com>
Date: Tue, 6 Nov 2012 13:36:53 +0100
Local: Tues, Nov 6 2012 7:36 am
Subject: Re: [Rails-core] Inconsistency in "constantize"
On Tue, Nov 6, 2012 at 12:37 PM, Aaron Patterson

Yeah, at first sight they seem to be different in their intention. The
new const_get is implemented as a recursive const_get, whereas
constantize is closer to emulate the resolution algorithm for constant
paths.

The subtle difference is in modules, as the test shows.

As you know, X::String raises a NameError if X is a module, and
resolves with a warning if X is a class that inherits from Object,
because Object is among its ancestors but the access is considered
dubious anyway. That's what constantize kinda emulates. I believe
"constantize" also raises if X is a class, and always treats the first
constant in the path as if it had a leading double colon "::", that's
why I say "kinda".

"Admin::UsersController".constantize fails if such controller does not
exist, regardless of the existence of an eventual top-level
UsersController, whereas if I understand it correctly
Object.const_get("Admin::UsersController", true) would resolve to such
top-level controller, because the original const_get name, inherit
does check Object by hand for modules, as the algorithm for relative
constant names does.

Not sure about changing that behavior.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Aaron Patterson  
View profile  
 More options Nov 6 2012, 6:51 pm
From: Aaron Patterson <tenderl...@ruby-lang.org>
Date: Tue, 6 Nov 2012 15:51:34 -0800
Local: Tues, Nov 6 2012 6:51 pm
Subject: Re: [Rails-core] Inconsistency in "constantize"

That is correct.  This implementation const_get would find class.

> Not sure about changing that behavior.

Neither am I, that's why I emailed. ;-)

It's a shame, I'm not actually sure what the new `const_get` buys us if
we can't replace this method. :'(

--
Aaron Patterson
http://tenderlovemaking.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »