Permalink
Browse files

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

  • Loading branch information...
1 parent 6b12d74 commit f7b0a857e97304a5daeb47313759b9bf0d7e2fc9 @jeremy jeremy committed Feb 25, 2010
@@ -1,6 +1,6 @@
require 'stringio'
require 'uri'
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
require 'rack/test'
module ActionDispatch
@@ -187,7 +187,7 @@ def reset!
unless defined? @named_routes_configured
# install the named routes in this session instance.
- klass = metaclass
+ klass = singleton_class
ActionDispatch::Routing::Routes.install_helpers(klass)
# the helpers are made protected by default--we make them public for
@@ -86,7 +86,7 @@ module ClassMethods
# AttributePerson.inheritance_column
# # => 'address_id'
def define_attr_method(name, value=nil, &block)
- sing = metaclass
+ sing = singleton_class
sing.send :alias_method, "original_#{name}", name
if block_given?
sing.send :define_method, name, &block
@@ -11,7 +11,7 @@
require 'active_support/core_ext/hash/indifferent_access'
require 'active_support/core_ext/hash/slice'
require 'active_support/core_ext/string/behavior'
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
require 'active_support/core_ext/module/delegation'
module ActiveRecord #:nodoc:
@@ -1,4 +1,4 @@
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
module ActiveRecord
# Exception that can be raised to stop migrations from going backwards.
@@ -303,7 +303,7 @@ def singleton_method_added(sym) #:nodoc:
case sym
when :up, :down
- metaclass.send(:alias_method_chain, sym, "benchmarks")
+ singleton_class.send(:alias_method_chain, sym, "benchmarks")
end
ensure
@ignore_new_methods = false
@@ -1,6 +1,6 @@
require 'active_support/core_ext/array'
require 'active_support/core_ext/hash/except'
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
module ActiveRecord
module NamedScope
@@ -112,7 +112,7 @@ def scope(name, options = {}, &block)
options.call(*args)
end, &block)
end
- metaclass.instance_eval do
+ singleton_class.instance_eval do
define_method name do |*args|
scopes[name].call(self, *args)
end
@@ -1,5 +1,7 @@
*Rails 3.0 (pending)*
+* Use Object#singleton_class instead of #metaclass. Prefer Ruby's choice. [Jeremy Kemper]
+
* JSON backend for YAJL. Preferred if available. #2666 [Brian Lopez]
@@ -1,7 +1,7 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/class/inheritable_attributes'
require 'active_support/core_ext/kernel/reporting'
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
module ActiveSupport
# Callbacks are hooks into the lifecycle of an object that allow you to trigger logic
@@ -312,7 +312,7 @@ def #{method_name}(&blk)
def _normalize_legacy_filter(kind, filter)
if !filter.respond_to?(kind) && filter.respond_to?(:filter)
- filter.metaclass.class_eval(
+ filter.singleton_class.class_eval(
"def #{kind}(context, &block) filter(context, &block) end",
__FILE__, __LINE__ - 1)
elsif filter.respond_to?(:before) && filter.respond_to?(:after) && kind == :around
@@ -1,4 +1,4 @@
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
require 'active_support/core_ext/module/delegation'
class Class
@@ -25,11 +25,12 @@ class Class
#
# Subclass.setting? # => false
def class_attribute(*attrs)
+ s = singleton_class
attrs.each do |attr|
- metaclass.send(:define_method, attr) { }
- metaclass.send(:define_method, "#{attr}?") { !!send(attr) }
- metaclass.send(:define_method, "#{attr}=") do |value|
- metaclass.send(:define_method, attr) { value }
+ s.send(:define_method, attr) { }
+ s.send(:define_method, "#{attr}?") { !!send(attr) }
+ s.send(:define_method, "#{attr}=") do |value|
+ singleton_class.send(:define_method, attr) { value }
end
end
end
@@ -1,6 +1,6 @@
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/array/extract_options'
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
class Class
def superclass_delegating_accessor(name, options = {})
@@ -11,9 +11,9 @@ def superclass_delegating_accessor(name, options = {})
# Generate the public methods name, name=, and name?
# These methods dispatch to the private _name, and _name= methods, making them
# overridable
- metaclass.send(:define_method, name) { send("_#{name}") }
- metaclass.send(:define_method, "#{name}?") { !!send("_#{name}") }
- metaclass.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) }
+ singleton_class.send(:define_method, name) { send("_#{name}") }
+ singleton_class.send(:define_method, "#{name}?") { !!send("_#{name}") }
+ singleton_class.send(:define_method, "#{name}=") { |value| send("_#{name}=", value) }
# If an instance_reader is needed, generate methods for name and name= on the
# class itself, so instances will be able to see them
@@ -27,12 +27,12 @@ def superclass_delegating_accessor(name, options = {})
# inheritance behavior, without having to store the object in an instance
# variable and look up the superclass chain manually.
def _stash_object_in_method(object, method, instance_reader = true)
- metaclass.send(:define_method, method) { object }
+ singleton_class.send(:define_method, method) { object }
define_method(method) { object } if instance_reader
end
def _superclass_delegating_accessor(name, options = {})
- metaclass.send(:define_method, "#{name}=") do |value|
+ singleton_class.send(:define_method, "#{name}=") do |value|
_stash_object_in_method(value, name, options[:instance_reader] != false)
end
self.send("#{name}=", nil)
@@ -6,6 +6,7 @@
require 'active_support/core_ext/object/conversions'
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'
require 'active_support/core_ext/object/extending'
@@ -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
@@ -1,4 +1,4 @@
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
require 'active_support/core_ext/module/aliasing'
module ActiveSupport
@@ -89,7 +89,7 @@ def test_subclasses_of_doesnt_find_anonymous_classes
end
end
-class ObjectTests < Test::Unit::TestCase
+class ObjectTests < ActiveSupport::TestCase
class DuckTime
def acts_like_time?
true
@@ -119,12 +119,16 @@ def test_duck_typing
assert !duck.acts_like?(:date)
end
- def test_metaclass
- string = "Hello"
- string.metaclass.instance_eval do
- define_method(:foo) { "bar" }
+ 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
- assert_equal "bar", string.foo
end
end
@@ -3,7 +3,7 @@
require 'active_support'
require 'active_support/core_ext/object/blank'
-require 'active_support/core_ext/object/metaclass'
+require 'active_support/core_ext/object/singleton_class'
require 'active_support/core_ext/array/extract_options'
require 'active_support/core_ext/hash/deep_merge'
require 'active_support/core_ext/module/attribute_accessors'
@@ -291,4 +291,4 @@ def self.namespaces_to_paths(namespaces) #:nodoc:
# If the application was already defined, configure generators,
# otherwise you have to configure it by hand.
-Rails::Generators.configure! if Rails.respond_to?(:application) && Rails.application
+Rails::Generators.configure! if Rails.respond_to?(:application) && Rails.application

0 comments on commit f7b0a85

Please sign in to comment.