Project

General

Profile

Bug #6374 » attr_module2.rb

Sega100500 (Сергей Е), 04/28/2012 01:53 PM

 
module Variables

def print_var
print "In module #{self.class.name}: #{@variable}\n"
end

def print_class_var
print "In module class-variable #{self.class.name}: #{@@class_variable}\n"
end

def set_variables(var, cvar)
@variable = var
@@class_variable = cvar
end

end


class A

include Variables

def initialize(var, cvar)
@variable = var
@@class_variable = cvar
end

def print_variables
print "variables: #{@variable}, #{@@class_variable}\n"
end

def self.print_class_a_variables
print "Class '#{self.class.name}' variable: #{@@class_variable}\n"
end

end

class B < A
end

a = A.new(123, 456)

A.print_class_a_variables
B.print_class_a_variables

a.print_variables

a.print_var

a.print_class_var # fail: :8:in `print_class_var': uninitialized class variable @@class_variable in Variables (NameError)
# Why? I set '@@class_variable` in 'initialize'

a.set_variables(12, 33) # Once again set '@@class_variavle'

a.print_class_var # But this method is tested
    (1-1/1)