Skip to content

Instantly share code, notes, and snippets.

@calavera
Created January 4, 2011 18:18
Show Gist options
  • Save calavera/765155 to your computer and use it in GitHub Desktop.
Save calavera/765155 to your computer and use it in GitHub Desktop.
From e548d8e62065232151e80bd1a58234885c14db64 Mon Sep 17 00:00:00 2001
From: David Calavera <[email protected]>
Date: Tue, 4 Jan 2011 19:16:28 +0100
Subject: [PATCH 1/2] Module.const_defined? accepts a flag to search into the receiver superclasses
---
core/module/const_defined_spec.rb | 8 ++++++++
1 files changed, 8 insertions(+), 0 deletions(-)
diff --git a/core/module/const_defined_spec.rb b/core/module/const_defined_spec.rb
index c8face6..d95ceef 100644
--- a/core/module/const_defined_spec.rb
+++ b/core/module/const_defined_spec.rb
@@ -19,6 +19,14 @@ describe "Module#const_defined?" do
# CS_CONST10 is defined in a module included by ChildA
ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST10).should be_true
end
+
+ it "returns false if the constant is defined in the receiver's superclass and the inherit flag is false" do
+ ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST4, false).should be_false
+ end
+
+ it "returns true if the constant is defined in the receiver's superclass and the inherit flag is true" do
+ ConstantSpecs::ContainerA::ChildA.const_defined?(:CS_CONST4, true).should be_true
+ end
end
it "returns true if the given String names a constant defined in the receiver" do
--
1.7.2.3
From be1b0d4bca3f588d8ec966c4e41c8bbd049e6040 Mon Sep 17 00:00:00 2001
From: David Calavera <[email protected]>
Date: Tue, 4 Jan 2011 19:16:46 +0100
Subject: [PATCH 2/2] Module.const_get accepts a flag to search into the receiver superclasses
---
core/module/const_get_spec.rb | 12 ++++++++++++
1 files changed, 12 insertions(+), 0 deletions(-)
diff --git a/core/module/const_get_spec.rb b/core/module/const_get_spec.rb
index c3b6641..d1fdd99 100644
--- a/core/module/const_get_spec.rb
+++ b/core/module/const_get_spec.rb
@@ -61,6 +61,18 @@ describe "Module#const_get" do
end.should raise_error(NameError)
end
+ ruby_version_is "1.9" do
+ it "raises a NameError if the constant is defined in the receiver's supperclass and the inherit flag is false" do
+ lambda do
+ ConstantSpecs::ContainerA::ChildA.const_get(:CS_CONST4, false)
+ end.should raise_error(NameError)
+ end
+
+ it "searches into the receiver superclasses if the inherit flag is true" do
+ ConstantSpecs::ContainerA::ChildA.const_get(:CS_CONST4, true).should == :const4
+ end
+ end
+
describe "with statically assigned constants" do
it "searches the immediate class or module first" do
ConstantSpecs::ClassA.const_get(:CS_CONST10).should == :const10_10
--
1.7.2.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment