ruby picture

RCR 252: Reverting the change in lookup of constants

Submitted by chneukirchen (Tue May 11 16:05:45 UTC 2004)

Abstract

In Ruby 1.6.8 the way constant lookup worked changed significantly. The dynamic way was changed into a more static way of lookup (see section Problem for an exact description).

This new way of constant lookup is confusing and didn't do what was promised, therefore it should be reverted.

Problem

Since Ruby 1.6.8 constant lookup works statically (examples from ruby-talk:59935 and following):

class Foo
  A = 1
end

def foo(&block)
  f = Foo.new
  f.instance_eval(&block)
end

foo do
  p self            #=> #<Foo:0x4027f568>
  p self.class::A   #=> 1
  p A               #=> 1 on 1.6.7; NameError on 1.6.8 and more recent.
end

This is especially in combination with instance_eval very confusing:

Object.new.instance_eval { B = 1; p B }
won't work.

This static behavior isn't something that's natural in a dynamic language and inconsistent as all other name are looked up dynamically.

Proposal

Revert the behavior of constant lookup as it used to be before 1.6.8.

Analysis

It was claimed that the change was made for "readability and for future optimization".

However, a large part of rubyists thinks its much more confusing and surprising, therefore claim 1, "readability" was not reached. Matz himself admitted in ruby-talk:60270 that the proclaimed performance boost is "Currently none; not much in the future."

The change could break code depending on the way of constant lookup.

Implementation

This used to be implemented until 1.6.8, so it is well possible.

However, it is not possible to implement this change in pure Ruby without a change to the interpreter.

ruby picture
Comments Current voting
Strongly opposed 2
Opposed 0
Neutral 1
In favor 3
Strongly advocate 5
ruby picture
If you have registered at RCRchive, you may now sign in below. If you have not registered, you may sign up for a username and password. Registering enables you to submit new RCRs, and vote and leave comments on existing RCRs.
Your username:
Your password:

ruby picture

Powered by .