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?

--
Posted via http://www.ruby-forum.com/.

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

···

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

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

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

···

On 12/04/2013, at 1:16 AM, Daniel ferrando <lists@ruby-forum.com> wrote:

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?

--
Posted via http://www.ruby-forum.com/\.

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?

···

--
Posted via http://www.ruby-forum.com/.

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.

     person1 += 1
     counter += 1
   elsif input == 20
     person2 += 1
     counter += 1
   elsif input == 30
     person3 += 1
     counter += 1
   else
     puts("Invalid input")
   end

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

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

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")

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

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.

···

Am 11.04.2013 17:16, schrieb Daniel ferrando:

--
<https://github.com/stomar/&gt;

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.

Quoting Daniel ferrando (lists@ruby-forum.com):

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

···

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

--
  * Se la Strada e la sua Virtu' non fossero state messe da parte,
* K * Carlo E. Prelz - fluido@fluido.as che bisogno ci sarebbe
  * di parlare tanto di amore e di rettitudine? (Chuang-Tzu)

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.

···

--
Posted via http://www.ruby-forum.com/\.

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

···

Am 11.04.2013 19:16, schrieb sto.mar@web.de:

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.

--
<https://github.com/stomar/&gt;

% ruby -e "p '1'.chomp.to_i == '1'.to_i"
true

···

On Apr 11, 2013, at 08:41 , Carlo E. Prelz <fluido@fluido.as> wrote:

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

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