我正在制作一个REPL,以便在应用程序中使用。我做了代码:
a = 1
b = 2
currentScope = []
Kernel.local_variables.each do |var|
    currentScope << [var,Kernel.eval(var.to_s)]
end
launchREPL(currentScope)在REPL内部,我可以执行以下代码:
@a     #=>1
@a+@b  #=>3理想情况下,在启动REPL之前,我不必编写这四行代码,而是希望在launchREPL函数中运行它们。但是,这需要从launchREPL函数内部访问前面的作用域。
Test1
最值得注意的是,我尝试过:
launchREPL(Kernel)当我做以下事情时:
def launchREPL(scope)
    F = 0
    puts scope.local_variables # => [:F]
end显然,这种方法是无效的。
Test2
launchREPL(Kernel.binding)
def launchREPL(scope)
    Kernel.binding.local_variables #= Error: private method 'local_variables' called for #<Binding>
end有办法做我想做的事吗?
编辑: P.S.这是当前launchREPL内部的代码:
def launchREPL(scope=nil,winName="Ruby REPL")
    # ICM RB file Begin:
    puts "\"Starting REPL...\""
    __b = binding   #Evaluating in a binding, keeps track of local variables
    __s = ""
    ###############################################################################
    # SEND INSTANCE VARIABLES TO REPL
    ###############################################################################
    #
    #How to prepare scope
    #   currentScope = []
    #   Kernel.local_variables.each do |var|
    #       currentScope << [var,Kernel.eval(var.to_s)]
    #   end
    #   launchREPL(currentScope)
    if scope != nil
        scope.each do |varDef|
            __b.instance_variable_set "@#{varDef[0].to_s}" , varDef[1]
            __b.eval("@#{varDef[0].to_s} = __b.instance_variable_get(:@#{varDef[0].to_s})")
        end
    end
    # to get instance variables: __b.instance_variable_get(__b.instance_variables[0])
    # or better:                 __b.instance_variable_get(:@pipe1)
    #
    ###############################################################################
    bStartup = true
    while bStartup || __s != ""
        # If startup required skip evaluation step
        if !bStartup
            #Evaluate command
            begin
                __ret = __s + "\n>" + __b.eval(__s).to_s
            rescue 
                __ret = __s + "\n> Error: " + $!.to_s
            end
            puts __ret
        else
            #REPL is already running
            bStartup = false
        end
        #Read user input & print previous output
        __s = WSApplication.input_box(__ret,winName,"")
        __s == nil ? __s = "" : nil
    end
end发布于 2017-04-27 14:13:45
这不符合我的评论,所以我会把它作为回答。
def launchREPL(scope = nil, winName = "Ruby REPL")
  puts '"Starting REPL..."'
  scope.eval('local_variables').each do |var|
    instance_variable_set "@#{var}", scope.eval(var.to_s)
  end if scope
  s = ""
  loop do
    ret = begin
            "#{s}\n> #{eval(s)}"
          rescue => e
            "#{s}\n> Error: #{e.message}"
          end
    puts ret
    # s = WSApplication.input_box(ret, winName, "")
    # break if s.empty?
    s = "100 * @a" # remove this line and uncomment 2 above
  end
end
a = 42
launchREPL(binding)这就是编写函数的方法(我只是让它看起来像红宝石代码一样)。上面的工作原理(目前它根本没有break,但是您可以看到它正在无限地计算4200 )。
发布于 2017-04-27 11:07:53
虽然您想要实现的目标还不清楚,而且确实有很多方法可以正确地实现,但是可以使用Object#send方法调用每个ruby方法:
def launchREPL(scope)
  scope.send :local_variables #⇒ here you go
end
a = 42
launchREPL(binding).include?(:a)
#⇒ trueSidenote:这就是你的“4行”通常是用红宝石写的:
local_variables.map { |var| [var, eval(var.to_s)] }这就是它们的编写方式(注Binding#local_variable_get):
local_variables.map { |var| [var, binding.local_variable_get(var)] }总结:
def launchREPL(scope)
  vars = scope.send(:local_variables).map do |var|
           [var, scope.local_variable_get(var)]
         end
  # some other code
end
a = 42
launchREPL(binding).to_h[:a]
#⇒ 42https://stackoverflow.com/questions/43655792
复制相似问题