Forum: Ruby confusion with gets, if

0cd11cc0cbd13de7ef8dd5dbdbf5f78b?d=identicon&s=25 Daniel ferrando (kiyoshi7)
on 2013-04-11 17:16
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?
3853dd5371ac1e094fc45d6c2aa0e459?d=identicon&s=25 Carlo E. Prelz (Guest)
on 2013-04-11 17:23
(Received via mailing list)
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
C993c6aaffc6cf7b477a5075099e1d36?d=identicon&s=25 Peter Hickman (Guest)
on 2013-04-11 17:29
(Received via mailing list)
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.
3131fcea0a711e5ad89c8d49cc9253b4?d=identicon&s=25 Julian Leviston (Guest)
on 2013-04-11 17:33
(Received via mailing list)
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
0cd11cc0cbd13de7ef8dd5dbdbf5f78b?d=identicon&s=25 Daniel ferrando (kiyoshi7)
on 2013-04-11 17:34
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?
3853dd5371ac1e094fc45d6c2aa0e459?d=identicon&s=25 Carlo E. Prelz (Guest)
on 2013-04-11 17:41
(Received via mailing list)
Subject: Re: confusion with gets, if
  Date: ven 12 apr 13 12:34:54 +0900

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
14b5582046b4e7b24ab69b7886a35868?d=identicon&s=25 Joel Pearson (virtuoso)
on 2013-04-11 17:44
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.
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-04-11 19:17
(Received via mailing list)
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.
15000f55138ae94b0f362ed7c625461a?d=identicon&s=25 unknown (Guest)
on 2013-04-11 22:20
(Received via mailing list)
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.

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
5a837592409354297424994e8d62f722?d=identicon&s=25 Ryan Davis (Guest)
on 2013-04-11 22:54
(Received via mailing list)
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.

% ruby -e "p '1'.chomp.to_i == '1'.to_i"
true
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.