Skip to content
This repository
Browse code

Added Object#metaclass

  • Loading branch information...
commit 98dd7226fa2fb306008fb5d89e38ac2391453664 1 parent 97ac788
Joshua Peek josh authored
1  activesupport/lib/active_support/core_ext/object.rb
... ... @@ -1,4 +1,5 @@
1 1 require 'active_support/core_ext/object/conversions'
2 2 require 'active_support/core_ext/object/extending'
3 3 require 'active_support/core_ext/object/instance_variables'
  4 +require 'active_support/core_ext/object/metaclass'
4 5 require 'active_support/core_ext/object/misc'
8 activesupport/lib/active_support/core_ext/object/metaclass.rb
... ... @@ -0,0 +1,8 @@
  1 +class Object
  2 + # Get object's meta (ghost, eigenclass, singleton) class
  3 + def metaclass
  4 + class << self
  5 + self
  6 + end
  7 + end
  8 +end
8 activesupport/test/core_ext/object_and_class_ext_test.rb
@@ -173,6 +173,14 @@ def test_duck_typing
173 173 assert duck.acts_like?(:time)
174 174 assert !duck.acts_like?(:date)
175 175 end
  176 +
  177 + def test_metaclass
  178 + string = "Hello"
  179 + string.metaclass.instance_eval do
  180 + define_method(:foo) { "bar" }
  181 + end
  182 + assert_equal "bar", string.foo
  183 + end
176 184 end
177 185
178 186 class ObjectInstanceVariableTest < Test::Unit::TestCase

0 comments on commit 98dd722

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