Change the context/binding inside a block in ruby

前端 未结 4 1521
盖世英雄少女心
盖世英雄少女心 2020-12-13 04:27

I have a DSL in Ruby that works like so:

desc \'list all todos\'
command :list do |c|
  c.desc \'show todos in long form\'
  c.switch :l
  c.action do |globa         


        
4条回答
  •  既然无缘
    2020-12-13 04:57

    Paste this code:

      def evaluate(&block)
        @self_before_instance_eval = eval "self", block.binding
        instance_eval &block
      end
    
      def method_missing(method, *args, &block)
        @self_before_instance_eval.send method, *args, &block
      end
    

    For more information, refer to this really good article here

提交回复
热议问题