Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

defines Module#anonymous?

  • Loading branch information...
commit b8bb54af7ff6652327d99f6a7cf5c96a3f3f876d 1 parent eea28b0
@fxn fxn authored
View
2  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'
View
20 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
View
14 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

1 comment on commit b8bb54a

@fxn
Owner

That predicate is not correct in 1.9, fixed later in http://github.com/rails/rails/commit/a506bac58616db2dc2b470a2416deed398645916

Please sign in to comment.
Something went wrong with that request. Please try again.