Project

General

Profile

Actions

Bug #1469

closed

Different behavior of class variables in 1.9.0 and 1.9.1p129

Added by biteme (Kenneth Ballou) almost 15 years ago. Updated almost 13 years ago.

Status:
Rejected
Target version:
ruby -v:
ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux]
Backport:
[ruby-core:23452]

Description

=begin
The code in the attached file (classvars.rb) is based on the discussion of class variables on pages 337 and 338 of "Programming Ruby 1.9". I ran the code using ruby 1.9.0 and ruby 1.9.1p129. The output was different for the two versions, and both differ from what the book says it should be.

For 1.9.0: ruby -v reports "ruby 1.9.0 (2006-06-08) [x86_64-linux]"
For 1.9.1: ruby -v reports "ruby 1.9.1p129 (2009-05-12 revision 23412) [i686-linux]"

Output from ruby 1.9.0:

a.var: 99
a.var: 123
a.get_var: top level variable
@@var: top level variable
Holder.read_var: 123

Just in case attaching the file classvars.rb doesn't work, here is its contents:

begin classvars.rb

class Holder
@@var = 99
def Holder.var=(val)
@@var = val
end
def Holder.read_var
@@var
end
def var
@@var
end
end

@@var = "top level variable"

a = Holder.new
puts "a.var: #{a.var}"
Holder.var = 123
puts "a.var: #{a.var}"

def a.get_var
@@var
end

puts "a.get_var: #{a.get_var}"
puts "@@var: #{@@var}"
puts "Holder.read_var: #{Holder.read_var}"

end classvars.rb

Output from ruby 1.9.1p129:

a.var: top level variable
a.var: 123
a.get_var: 123
@@var: 123
Holder.read_var: 123

If I understand the book correctly, the expected output would be:

a.var: top level variable
a.var: 123
a.get_var: top level variable
@@var: top level variable
Holder.read_var: 123

(I'm not sure I understand why the first call to a.var would return the string "top level variable" but the second call would return the Fixnum 123. However, that's not part of this issue report.)
=end


Files

classvars.rb (400 Bytes) classvars.rb biteme (Kenneth Ballou), 05/15/2009 04:26 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0