本文介绍了Ruby,不会将Symbol隐式转换为Integer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

yesterday I already asked you in "no implicit conversion of Symbol into Integer, Ruby". I think you need further information to answer the question. That’s the reason, why I asked again. I updated my system from ruby 1.8.7 to the newer version of ruby 2.3.1p112.

当我要运行测试时,总是会收到错误消息:致命:没有将Symbol隐式转换为Integer

When I want to run a test, I always get the error: 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行,则不会收到错误消息.

If I comment the 3 lines in RubyCommand, I don’t get the error.

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

问题出在数组和哈希值上.但是我不知道如何运行此代码.

The problem is something with array and hash. But I have no idea how to get this code running.

推荐答案

您创建的RubyCommand实例类似

Litu::RubyCommand.new("switch_wago_do")

您有

def initialize(params, &block)

所以params将是等于"switch_wago_do"的字符串.
但是您希望它是Hash实例.

So params would be String equal "switch_wago_do".
But you expect it to be Hash instance.

这就是为什么注释这些字符串可以解决问题的原因.

That is why commenting these strings solves problem.

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

这篇关于Ruby,不会将Symbol隐式转换为Integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 16:32