With object

先写一个方法。

 

  def with(object, &block)
    object.instance_eval(&block)
  end

 

示例:

 

    numbers = [1, 2, 3]

    with numbers do
      map! { |n| n + 100 }
      reject! { |n| n % 2 == 0 }
    end

    p numbers # => [101, 103]

    n = 15
    n = with n do
      self + 13
    end
    p n # => 28

 

你可能感兴趣的:(object)