Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 3469

Show
Ignore:
Timestamp:
01/23/06 06:40:43 (4 years ago)
Author:
sam
Message:

Add Object#instance_exec and Proc#bind

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activesupport/CHANGELOG

    r3468 r3469  
    11*SVN* 
     2 
     3* Add Object#instance_exec, like instance_eval but passes its arguments to the block.  (Active Support will not override the Ruby 1.9 implementation of this method.) [Sam Stephenson] 
     4 
     5* Add Proc#bind(object) for changing a proc or block's self by returning a Method bound to the given object. Based on why the lucky stiff's "cloaker" method. [Sam Stephenson] 
    26 
    37* Fix merge and dup for hashes with indifferent access #3404 [kenneth.miller@bitfield.net] 
  • trunk/activesupport/lib/active_support/core_ext/object_and_class.rb

    r3356 r3469  
    6666    ActiveSupport::JSON.encode(self) 
    6767  end 
     68   
     69  unless defined? instance_exec # 1.9 
     70    def instance_exec(*arguments, &block) 
     71      block.bind(self)[*arguments] 
     72    end 
     73  end 
    6874end 
    6975 
  • trunk/activesupport/test/core_ext/object_and_class_ext_test.rb

    r3356 r3469  
    112112    assert_equal({'a' => 1, 'b' => 2}, object.instance_values) 
    113113  end 
     114   
     115  def test_instance_exec_passes_arguments_to_block 
     116    block = Proc.new { |value| [self, value] } 
     117    assert_equal %w(hello goodbye), 'hello'.instance_exec('goodbye', &block) 
     118  end 
     119   
    114120end