From 63a7ef0d745dac45829d63ed258b90ae0df7d593 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Thu, 25 Feb 2010 09:32:29 -0800 Subject: [PATCH] Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. --- activerecord/lib/active_record/dirty.rb | 2 +- activesupport/CHANGELOG | 2 ++ .../lib/active_support/core_ext/object.rb | 1 + .../active_support/core_ext/object/metaclass.rb | 11 ++++++----- .../test/core_ext/object_and_class_ext_test.rb | 14 +++++++++++++- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/activerecord/lib/active_record/dirty.rb b/activerecord/lib/active_record/dirty.rb index f18965169d..ef0f3b81c4 100644 --- a/activerecord/lib/active_record/dirty.rb +++ b/activerecord/lib/active_record/dirty.rb @@ -167,7 +167,7 @@ module ActiveRecord 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) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index 0e763ce079..440c6e7287 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -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] diff --git a/activesupport/lib/active_support/core_ext/object.rb b/activesupport/lib/active_support/core_ext/object.rb index f140be3c8d..65bc222b80 100644 --- a/activesupport/lib/active_support/core_ext/object.rb +++ b/activesupport/lib/active_support/core_ext/object.rb @@ -3,4 +3,5 @@ require 'active_support/core_ext/object/conversions' 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' diff --git a/activesupport/lib/active_support/core_ext/object/metaclass.rb b/activesupport/lib/active_support/core_ext/object/metaclass.rb index 93fb0ad594..4b36a243c9 100644 --- a/activesupport/lib/active_support/core_ext/object/metaclass.rb +++ b/activesupport/lib/active_support/core_ext/object/metaclass.rb @@ -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 diff --git a/activesupport/test/core_ext/object_and_class_ext_test.rb b/activesupport/test/core_ext/object_and_class_ext_test.rb index b6515e05a0..fcaaeb4ca4 100644 --- a/activesupport/test/core_ext/object_and_class_ext_test.rb +++ b/activesupport/test/core_ext/object_and_class_ext_test.rb @@ -107,7 +107,7 @@ class ClassExtTest < Test::Unit::TestCase 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 @@ class ObjectTests < Test::Unit::TestCase 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