-7

I'm new to ruby, and I'm trying to recreate a script I made in python, in ruby, to learn. So far, I feel like this is pretty simple, but the program simply will not run. I'm getting this error

"syntax error, unexpected end-of-input, expecting keyword_end"

I'm not sure why, I ended my loop with an end

This is the code

#ruby version of work script

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

print "RA1: " + "#{a}" + "RS1: " + "#{b}" + "RF4: " + "#{c}"`
AVI
  • 5,516
  • 5
  • 29
  • 38
  • 2
    All of those `if` blocks need an `end` after them. Also, see http://stackoverflow.com/questions/948135/how-can-i-write-a-switch-statement-in-ruby – user513951 Jan 17 '16 at 05:45
  • @gabriel - Alternative to nested if else you can use case statement. – RockStar Jan 18 '16 at 11:40

1 Answers1

3

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}"
Atri
  • 5,511
  • 5
  • 30
  • 40
  • thank you, that was extremely helpful. How would I add multiple arguments to the elsif statements? Like it would be this in python `elif i in ("RA1", "RA2")` and also for the while statement, could I type in `while i not in ("END", "end", "End")` in ruby? thanks – gabriel scharf Jan 17 '16 at 06:18
  • you could do: `["RA1", "RA2"].include?(i)` to test something like `i in ("RA1", "RA2")` and for `not in` you could do `!["RA1", "RA2"].include?(i)` – Atri Jan 17 '16 at 06:20