shube shube - 4 months ago 28
Ruby Question

Ruby, no implicit conversion of Symbol into Integer

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.

When I want to run a test, I always get the error: FATAL: no implicit conversion of Symbol into Integer

Here is the code:

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


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.

Answer

You create RubyCommandinstance like

Litu::RubyCommand.new("switch_wago_do")

and you have

def initialize(params, &block)

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