[Solved] For some reason, this Ruby script is not working


There are multiple issues with your code:

  • You have syntax errors. You need end after each of your if and else statements unlike python.
  • From your code it looks like you are looking for the if-elsif statement and not the multiple if statements because the else statement will be of the last if.
  • You need to put i = gets.chomp inside the while loop so that you don’t go into an infinite loop.

Try something like this:

#ruby version of work script

a = 0
b = 0
c = 0
i = " "
puts "Hello, please input the 3 character code."

while i != "END"
  i = gets.chomp
  if i == "RA1"
    a += 1
  elsif i == "RS1"
    b += 1
  elsif i == "RF4"
    c += 1
  elsif i == "END"
    print "Complete"
  else
    puts "Please enter the 3 character code"
  end
end

print "RA1: " + "#{a}" + "RS1: " + "#{b}" + "RF4: " + "#{c}"

2

solved For some reason, this Ruby script is not working