【问题标题】:Ruby, no implicit conversion of Symbol into IntegerRuby,没有将 Symbol 隐式转换为 Integer
【发布时间】:2016-07-21 09:20:17
【问题描述】:

昨天我已经在“no implicit conversion of Symbol into Integer, Ruby”问过你。我认为您需要更多信息来回答这个问题。这就是原因,我又问了一遍。我将系统从 ruby​​ 1.8.7 更新到了更新版本的 ruby​​ 2.3.1p112。

当我想运行测试时,我总是得到错误:FATAL: no implicit conversion of Symbol into Integer

代码如下:

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 实例?
  • 最好修改原始问题,而不是创建新问题。 (与此同时,另一个问题被标记为重复)。
  • 在 element_Switch_wago_do 我这样创建实例: RubyCommand.new("switch_wago_do") do |cmd, context|而 RubyCommand 是在同一个脚本中定义的一个类

标签: ruby implicit-conversion


【解决方案1】:

你创建RubyCommandinstance 喜欢

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]||"."

【讨论】:

    猜你喜欢
    • 2014-03-12
    • 2020-08-14
    • 1970-01-01
    • 2019-01-19
    • 1970-01-01
    • 2016-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多