Home>

About ruby ​​programs.
・ SET i a: Assign value a to variable i (i = 1, 2)
・ ADD a: Calculates the value of variable 1 + a and assigns the calculation result to variable 2
・ SUB a: Calculates the value of variable 1-a and assigns the calculation result to variable 2
The first line is given the number of instructions n.
The following n lines are given in the order in which n instructions are executed.

n
s_1
s_2

s_i is one of three strings:
・ SET i a
・ ADD a
・ SUB a
(i = 1, 2;a is an integer)
Each value is passed as a string from standard input. I was thinking about a program like this, but I can't help it and I'd like to ask you for advice on what went wrong.

Code
input_lines = gets.to_i
i = Array.new (2, 0)

def setf (a, b)
i [a-1] = b
end

def addf (a)
i [1] = i [0] + a
end

def subf (a)
i [1] = i [0]-a
end

input_lines.times do | n |
command = gets.chomp.split ('')
if command [0] =='SET'
setf (command [1] .to_i, command [2] .to_i)
elsif command [0] =='ADD'
addf (command [1] .to_i)
else
subf (command [1] .to_i)
end
end

puts i.join ('')

Error
Main.rb: 9: insetf&apos ;: undefined local variable or methodi'for main: Object (NameError)
from Main.rb: 23: inblock in<main>'     from Main.rb: 20: intimes'
from Main.rb: 20: in `<main>'