Permalink
Browse files

AS guides: explains Object#metaclass and Object#class_eval

  • Loading branch information...
fxn committed Jun 12, 2009
1 parent b9ac947 commit e20e0c8c4260e3718a77367f1b3d7546305defaa
Showing with 27 additions and 0 deletions.
  1. +27 −0 railties/guides/source/active_support_overview.textile
@@ -51,6 +51,33 @@ The method +present?+ is equivalent to +!blank?+:
assert @response.body.present? # same as !@response.body.blank?
</ruby>
+h4. +metaclass+
+
+The method +metaclass+ returns the singleton class on any object:
+
+<ruby>
+String.metaclass # => #<Class:String>
+</ruby>
+
+h4. +class_eval(*args, &block)+
+
+You can evaluate code in the context of any object's singleton class using +class_eval+:
+
+<ruby>
+class Proc
+ def bind(object)
+ block, time = self, Time.now
+ object.class_eval do
+ method_name = "__bind_#{time.to_i}_#{time.usec}"
+ define_method(method_name, &block)
+ method = instance_method(method_name)
+ remove_method(method_name)
+ method
+ end.bind(object)
+ end
+end
+</ruby>
+
h3. Extensions to +Module+
...

0 comments on commit e20e0c8

Please sign in to comment.