Change the binding of a Proc in Ruby

后端 未结 4 1507
春和景丽
春和景丽 2020-12-15 05:48

I have this code:

 l = lambda { a }
 def some_function
     a = 1
 end

I just want to access a by the lambda and a special sco

4条回答
  •  臣服心动
    2020-12-15 06:26

    a similar way:

    class Context
      attr_reader :_previous, :_arguments
    
      def initialize(_previous, _arguments)
        @_previous = _previous
        @_arguments = _arguments
      end
    end
    
    def _code_def(_previous, _arguments = [], &_block)
      define_method("_code_#{_previous}") do |_method_previous, _method_arguments = []|
        Context.new(_method_previous, _method_arguments).instance_eval(&_block)
      end
    end
    
    _code_def('something') do
      puts _previous
      puts _arguments
    end
    

提交回复
热议问题