Ruby,没有将Symbol隐式转换为Integer

昨天我已经问你“ 没有将Symbol隐式转换为整数,Ruby ”。 我认为您需要更多信息来回答这个问题。 这就是原因,为什么我再问一次。 我将我的系统从ruby 1.8.7更新到了更新版本的ruby 2.3.1p112。

当我想运行测试时,我总是得到错误: 致命:没有将Symbol隐式转换为整数

这是代码:

def element_switch_wago_do(step) raise Rutema::ParserError, "Missing DO tag!" unless step.has_do? raise Rutema::ParserError, "Missing DO value!" unless step.has_value? raise Rutema::ParserError, "Used DO value '#{step.value}' not supported [0||1 valid]!" unless ((0 == step.value.to_i) || (step.value.to_i == 1)) step.txt="Switch Wago DIGITAL output-pin #{step.do} to #{step.value}" ip = "{WAGO_IP}" port = "{WAGO_PORT}" step.cmd = Litu::RubyCommand.new("switch_wago_do") do |cmd, context| Litu::subst_template!(ip, context) Litu::subst_template!(port, context) Litu::subst_template!(step.do, context) ModBus::TCPClient.new(ip, port.to_i) do |cl| cl.with_slave(1) do |slave| slave.debug = false slave.write_single_coil(step.do.to_i,step.value.to_i) end end end end class RubyCommand include Patir::Command attr_reader :cmd,:working_directory,:killProc def initialize params,&block @killProc=params[:killProc] @name=params[:name] @working_directory=params[:working_directory]||"." if block_given? @cmd=block else raise "You need to provide a block" end end #Runs the associated block def run context=nil @run=true begin t1=Time.now cmd = @cmd pwd = @working_directory p = Dir.pwd puts "######: #{cmd}:" Litu::subst_template!(pwd, context) puts "before block in dir #{Dir.pwd}" Dir.chdir(pwd) do p = Dir.pwd puts "in block in dir #{cmd}" @cmd.call(self, context) @status=:success end puts ":###### #{p}" rescue StandardError error << "\n#{$!.message}" error << "\n#{$!.backtrace}" if $DEBUG @status=:error ensure @exec_time=Time.now-t1 end return @status end def kill! @killProc.call if @killProc end def to_s return @name end end 

如果我在RubyCommand中评论3行,我就不会收到错误。

 #@killProc=params[:killProc] #@name=params[:name] #@working_directory=params[:working_directory]||"." 

问题是数组和哈希。 但我不知道如何运行此代码。

您可以创建RubyCommand实例

 Litu::RubyCommand.new("switch_wago_do") 

你有

 def initialize(params, &block) 

因此params将字符串等于"switch_wago_do"
但是你希望它是Hash实例。

这就是为什么评论这些字符串可以解决问题。

 @killProc=params[:killProc] @name=params[:name] @working_directory=params[:working_directory]||"."