어제 이미 "no implicit conversion of Symbol into Integer, Ruby"이라고 물었습니다. 질문에 대답하기 위해 추가 정보가 필요하다고 생각합니다. 그것이 내가 다시 물었던 이유입니다. 내 시스템을 루비 1.8.7에서 최신 버전의 루비 2.3.1p112로 업데이트했다. 나는 3를 언급하는 경우Ruby, 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
:
내가 테스트를 실행할 때, 나는 항상 오류 얻을 : FATAL : 코드 여기
정수에 기호없이 암시 적 변환입니다 RubyCommand의 행을 사용하면 오류가 발생하지 않습니다.
#@killProc=params[:killProc]
#@name=params[:name]
#@working_directory=params[:working_directory]||"."
문제는 배열 및 해시입니다. 하지만이 코드를 어떻게 실행 시킬지 전혀 모르겠습니다.
어떻게 'RubyCommand' 인스턴스를 만드나요? – Aleksey
새 질문을 만드는 대신 원래 질문을 더 잘 수정하십시오. (그 동안 다른 질문은 중복으로 표시됩니다.) – knut
in element_Switch_wago_do 다음과 같이 인스턴스를 만듭니다. RubyCommand.new ("switch_wago_do") do | cmd, context | RubyCommand는 동일한 스크립트에서 정의 된 클래스입니다. – shube