aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorXavier Noria <fxn@hashref.com>2010-02-11 23:15:04 +0100
committerXavier Noria <fxn@hashref.com>2010-02-11 23:15:04 +0100
commitb8bb54af7ff6652327d99f6a7cf5c96a3f3f876d (patch)
treead12a39f6056b0fee2f2803dfb6b14c5e56eb42d /activesupport
parenteea28b07cf25d5c79f8edeb8bef5e0483b489425 (diff)
downloadrails-b8bb54af7ff6652327d99f6a7cf5c96a3f3f876d.tar.gz
rails-b8bb54af7ff6652327d99f6a7cf5c96a3f3f876d.tar.bz2
rails-b8bb54af7ff6652327d99f6a7cf5c96a3f3f876d.zip
defines Module#anonymous?
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/module.rb2
-rw-r--r--activesupport/lib/active_support/core_ext/module/anonymous.rb20
-rw-r--r--activesupport/test/core_ext/module/anonymous_test.rb14
3 files changed, 35 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/core_ext/module.rb b/activesupport/lib/active_support/core_ext/module.rb
index 41600929f4..c907445d90 100644
--- a/activesupport/lib/active_support/core_ext/module.rb
+++ b/activesupport/lib/active_support/core_ext/module.rb
@@ -1,6 +1,6 @@
require 'active_support/core_ext/module/aliasing'
require 'active_support/core_ext/module/introspection'
-
+require 'active_support/core_ext/module/anonymous'
require 'active_support/core_ext/module/attribute_accessors'
require 'active_support/core_ext/module/attr_internal'
require 'active_support/core_ext/module/attr_accessor_with_default'
diff --git a/activesupport/lib/active_support/core_ext/module/anonymous.rb b/activesupport/lib/active_support/core_ext/module/anonymous.rb
new file mode 100644
index 0000000000..60d7d9410b
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/module/anonymous.rb
@@ -0,0 +1,20 @@
+class Module
+ # A module may or may not have a name.
+ #
+ # module M; end
+ # M.name # => "M"
+ #
+ # m = Module.new
+ # m.name # => ""
+ #
+ # A module gets a name when it is first assigned to a constant. Either
+ # via the +module+ or +class+ keyword or by an explicit assignment:
+ #
+ # m = Module.new # creates an anonymous module
+ # M = m # => m gets a name here as a side-effect
+ # m.name # => "M"
+ #
+ def anonymous?
+ name == ''
+ end
+end
diff --git a/activesupport/test/core_ext/module/anonymous_test.rb b/activesupport/test/core_ext/module/anonymous_test.rb
new file mode 100644
index 0000000000..7a78a3b012
--- /dev/null
+++ b/activesupport/test/core_ext/module/anonymous_test.rb
@@ -0,0 +1,14 @@
+require 'abstract_unit'
+require 'active_support/core_ext/module/anonymous'
+
+class AnonymousTest < ActiveSupport::TestCase
+ test "an anonymous class or module are anonymous" do
+ assert Module.new.anonymous?
+ assert Class.new.anonymous?
+ end
+
+ test "a named class or module are not anonymous" do
+ assert !Kernel.anonymous?
+ assert !Object.anonymous?
+ end
+end \ No newline at end of file