Project

General

Profile

Actions

Bug #4484

closed

Class variables leak to Object when using class_eval

Added by pawel (Paweł P) about 13 years ago. Updated almost 5 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 1.8.7 (2009-06-12 patchlevel 174) [i486-linux]
[ruby-core:<unknown>]

Description

=begin
The following irb session describes the problem:

String.class_eval { @@string_class_variable = 'should be only in String' }
=> "should be only in String"
"WTF?" if Object.class_variables.include? "@@string_class_variable"
=> "WTF?"

Tested on 1.8 and 1.9.
I've created the issue in "ruby-18" project because "ruby" project doesn't allow to create new issues.
=end

Updated by jeremyevans0 (Jeremy Evans) almost 5 years ago

  • Project changed from Ruby 1.8 to Ruby master
  • Description updated (diff)
  • Status changed from Open to Rejected
  • Backport set to 2.5: UNKNOWN, 2.6: UNKNOWN

This is still true in master, but I believe this is expected behavior/spec. This is similar to constants:

String.class_eval {A = 1}
Object::A
# => 1

If you want to scope constants or class variables, you need to reopen the class using class syntax:

class String
  @@string_class_variable = 'should be only in String'
  A = 1
end
Object.class_variables.include? :@@string_class_variable
# => false
Object::A
# NameError (uninitialized constant A)
Actions

Also available in: Atom PDF

Like0
Like0