代码之家  ›  专栏  ›  技术社区  ›  Sebastien Tanguy

翻译DSL的块和语句

  •  4
  • Sebastien Tanguy  · 技术社区  · 17 年前

    some_function {
        t + 2
    }
    

    1.upto(10) {|x|
        some_function {
            t + x
        }
    }
    

    更新:

    [:dvar, :x]
    

    3 回复  |  直到 17 年前
        1
  •  1
  •   Justin Love    17 年前

    def some_function(&block)
      b = block.binding
      p [b.eval("t"), b.eval("x")]
    end
    
    t = 1
    1.upto(10) {|x|
      some_function {
        t + x
      }
    }
    
        2
  •  0
  •   flicken    17 年前

    因此,该块不能(也不能!)

    method_missing t x

        3
  •  0
  •   Justin Love    17 年前

    t .

    class Context
      attr_accessor :t
    
      def initialize(_t)
        @t = _t
      end
    end
    
    def some_function(&block)
      puts Context.new(1).instance_eval(&block)
    end
    
    1.upto(10) {|x|
      some_function {
        t + x
      }
    }
    
    推荐文章