Permalink
Browse files

Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice.

  • Loading branch information...
1 parent 3a3fa7f commit 63a7ef0d745dac45829d63ed258b90ae0df7d593 @jeremy jeremy committed Feb 25, 2010
@@ -167,7 +167,7 @@ def field_changed?(attr, old, value)
module ClassMethods
def self.extended(base)
- base.metaclass.alias_method_chain(:alias_attribute, :dirty)
+ base.singleton_class.alias_method_chain(:alias_attribute, :dirty)
end
def alias_attribute_with_dirty(new_name, old_name)
View
@@ -1,5 +1,7 @@
*2.3.6 (pending)*
+* Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. [Jeremy Kemper]
+
* JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez]
* Introduce String#html_safe for rails_xss plugin and forward-compatibility with Rails 3. [Michael Koziarski, Santiago Pastorino, José Ignacio Costa]
@@ -3,4 +3,5 @@
require 'active_support/core_ext/object/extending'
require 'active_support/core_ext/object/instance_variables'
require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
require 'active_support/core_ext/object/misc'
@@ -1,13 +1,14 @@
+require 'active_support/deprecation'
+
class Object
- # Get object's meta (ghost, eigenclass, singleton) class
+ # Get object's meta (ghost, eigenclass, singleton) class.
+ #
+ # Deprecated in favor of Object#singleton_class.
def metaclass
class << self
self
end
end
- # If class_eval is called on an object, add those methods to its metaclass
- def class_eval(*args, &block)
- metaclass.class_eval(*args, &block)
- end
+ deprecate :metaclass => :singleton_class
end
@@ -107,7 +107,7 @@ def test_subclasses_of_doesnt_find_anonymous_classes
end
end
-class ObjectTests < Test::Unit::TestCase
+class ObjectTests < ActiveSupport::TestCase
def test_suppress_re_raises
assert_raise(LoadError) { suppress(ArgumentError) {raise LoadError} }
end
@@ -176,6 +176,18 @@ def test_metaclass
end
assert_equal "bar", string.foo
end
+
+ def test_singleton_class
+ o = Object.new
+ assert_equal class << o; self end, o.singleton_class
+ end
+
+ def test_metaclass_deprecated
+ o = Object.new
+ assert_deprecated /use singleton_class instead/ do
+ assert_equal o.singleton_class, o.metaclass
+ end
+ end
end
class ObjectInstanceVariableTest < Test::Unit::TestCase

0 comments on commit 63a7ef0

Please sign in to comment.