Skip to content

Commit

Permalink
Setup the scope when assigning a class to constant
Browse files Browse the repository at this point in the history
fixes #1054
fixes #1056
  • Loading branch information
elia committed Aug 12, 2015
1 parent 73ce13e commit d9dee02
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
10 changes: 7 additions & 3 deletions opal/corelib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,7 @@
const_scope.constants = [];

if (id) {
klass.$$orig_scope = base;
base[id] = base.constructor[id] = klass;
base.constants.push(id);
Opal.cdecl(base, id, klass)
}
}

Expand Down Expand Up @@ -612,6 +610,12 @@
* constant decl
*/
Opal.cdecl = function(base_scope, name, value) {
if (value.$$is_class && value.$$orig_scope == null) {
value.$$name = name;
value.$$orig_scope = base_scope;
base_scope.constructor[name] = value;
}

base_scope.constants.push(name);
return base_scope[name] = value;
};
Expand Down
22 changes: 22 additions & 0 deletions spec/opal/core/language/class_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
describe "Assigning Class.new to a constant" do
klass = Class.new do
def bar
:bar
end
end
ConstantWithAssignedClass = klass
class ConstantWithAssignedClass
def foo
:foo
end
end

it "sets the class' name" do
ConstantWithAssignedClass.name.should == 'ConstantWithAssignedClass'
end

it "can be reopened by the constant name" do
ConstantWithAssignedClass.new.foo.should == :foo
ConstantWithAssignedClass.new.bar.should == :bar
end
end

0 comments on commit d9dee02

Please sign in to comment.