diff --git a/language/class_variable_spec.rb b/language/class_variable_spec.rb index dffab47a6b..231d613a21 100644 --- a/language/class_variable_spec.rb +++ b/language/class_variable_spec.rb @@ -82,3 +82,24 @@ c.class_variable_get(:@@cv).should == :next end end + +ruby_version_is "3.0" do + describe 'Class variable overtaking' do + it "accessing a class variable from the toplevel scope raises a RuntimeError" do + -> do + eval <<-CODE + @@cvar_a = :new_val + CODE + end.should raise_error(RuntimeError, /class variable access from toplevel/) + end + + it "raises a RuntimeError when a class variable is overtaken by the same definition in an ancestor class" do + a = Class.new() + b = Class.new(a) + b.class_variable_set(:@@cvar, :value) + a.class_variable_set(:@@cvar, :new_value) + + -> { b.class_variable_get(:@@cvar) }.should raise_error(RuntimeError) + end + end +end