Confusion with gets, if

hi I’m new to ruby and im trying to write some simple codes but i cant
get the 'if’s to work. below what i mean. how can i fix the ruby code
for the ifs to work?

no matter what i write it always returns “invalid input”
##################Code#########################
while counter < 1 do
print ("put 10, 20 or 30 ")
input = gets()
if input == ‘10’
person1 += 1
counter += 1
elsif input == 20
person2 += 1
counter += 1
elsif input == 30
person3 += 1
counter += 1
else
puts(“Invalid input”)
end

end
##############################################

if i were to write it in python i would do it somewhat like this:

##################Code#########################
while counter < 5 :

vector.append(input('put 10, 20 or 30 '))

if vector [counter] == '10' :
    p1 += 1
    counter += 1
elif vector [counter] == '20':
    p2 += 1
    counter +=1
elif vector [counter] == '30':
    p3 += 1
    counter += 1
else:
    print ("invalid input")

##############################################

last question whats the difference between ruby and ruby on rails?

If you put “p input” just after you get the input you will see “10\n”.
That
is the newline on the end. Remove it with

input = gets.chomp # removes the “\n”

It’s a common beginners problem with Ruby. A string from input will
include
the new line on the end and it either needs to be removed (with chomp)
or
handled in some way.

Subject: confusion with gets, if
Date: ven 12 apr 13 12:16:22 +0900

hi I’m new to ruby and im trying to write some simple codes but i cant
get the 'if’s to work. below what i mean. how can i fix the ruby code
for the ifs to work?

Here:

if input == ‘10’

you check against a string, but here:

elsif input == 20

and here:

elsif input == 30

you check against an integer… I don’t know about the rest.

Carlo

In ruby, gets() will read a line, which generally includes a newline
character.

the first thing you could do to detect this on your own is write a
“debugging line” just after the input = gets() line to see what “input”
is set to

thus:

input = gets()
puts input.inspect

Then when you run your program, it’ll show you what input is, and
therefore you’ll see why your ifs aren’t working.

But yeah, you’re using integers sometimes, strings other things… and
if you want to get rid of the newline, change your input line to this:

input = gets.chomp

Julian

Subject: Re: confusion with gets, if
Date: ven 12 apr 13 12:34:54 +0900

Quoting Daniel ferrando ([email protected]):

the “if input == ‘10’” was my attempt in fixing it

It was in the correct direction, since gets() returns a string, not an
integer.

Instead of

input = gets()

try

input=gets().chomp().to_i()

chomp() removes the carriage return, and to_i() converts the string to
an integer.

Carlo

Daniel ferrando wrote in post #1105282:

how do i stop the “program” if its
in an infinite loop without closing the command promt itself?

Usually Ctrl + C is the shortcut to close a command-prompt program.

the “if input == ‘10’” was my attempt in fixing it

all this code does is count how many times the user wrote 10, 20 and 30
here is the whole thing. no matter what im typing it only gives me
invalid input so this loops on forever

person1 = (0.0)
person2 = (0.0)
person3 = (0.0)
counter = (0)
input = (0)
list = []

while counter < 1 do
print ("put 10, 20 or 30 ")
input = gets()
if input == 10
person1 += 1
counter += 1
elsif input == 20
person2 += 1
counter += 1
elsif input == 30
person3 += 1
counter += 1
else
puts(“Invalid input”)
end

end

puts(list)
puts(person1)
puts(person2)
puts(person3)

ps im running this on command promt how do i stop the “program” if its
in an infinite loop without closing the command promt itself?

Am 11.04.2013 17:16, schrieb Daniel ferrando:

hi I’m new to ruby and im trying to write some simple codes but i cant
get the 'if’s to work. below what i mean. how can i fix the ruby code
for the ifs to work?

no matter what i write it always returns “invalid input”
##################Code#########################
while counter < 1 do
print ("put 10, 20 or 30 ")

here, the extra space is not a problem, but you should use

print("put 10, 20 or 30 ")

or simply: print "put 10, 20 or 30 "

input = gets()
if input == ‘10’

The source of the problem can be seen here:

2.0.0-p0 :001 > input = gets
10
=> “10\n”

the returned string contains an new line character!

possible solutions:

  1. input = gets.chomp # the parentheses are not needed
    if input == ‘10’

  2. input = gets
    if input == “10\n” # mind the double quotes!

And: it’s always good practice to try code using IRB.

end

end
##############################################

As mentioned in a different post you would need strings
for 20 and 30, too.

 elif vector [counter] == '20':
     p2 += 1
     counter +=1
 elif vector [counter] == '30':
     p3 += 1
     counter += 1
 else:
     print ("invalid input")

##############################################

I’m not exactly sure what you are trying to do, but I don’t care
much for the while loop and all the counters.

last question whats the difference between ruby and ruby on rails?

Rails is a web framework written in Ruby, maybe comparable to Django
in the Python world.

On Apr 11, 2013, at 08:41 , Carlo E. Prelz [email protected] wrote:

input=gets().chomp().to_i()

chomp() removes the carriage return, and to_i() converts the string to
an integer.

% ruby -e “p ‘1’.chomp.to_i == ‘1’.to_i”
true

Am 11.04.2013 19:16, schrieb [email protected]:

I’m not exactly sure what you are trying to do, but I don’t care
much for the while loop and all the counters.

I got all the answers to your original questions just now,
including your complete example.

A different implementation:

counts = [0, 0, 0]
total = 0

while total < 5 do
answer = gets.chomp

case answer
when ‘10’
counts[0] += 1
when ‘20’
counts[1] += 1
when ‘30’
counts[2] += 1
else
puts “Try again”
end

total = counts.inject(:+)
end

puts counts