Ok so i am trying to get my if statement to work but my console keeps
closing out and i don’t know how to keep it open other than using gets
which doesn’t seem to work when there’s an error, the c++ equivalent of
keeping the console open is cin.get() which waits for user input, is
gets the ruby equivalent or is there another way to keep it open no
matter what? but anyways here is my code:
print “Please enter your age”
age = gets
if age < 18
print “you are younger than 18”
end
if age > 18
print “you are older than 18”
end
gets
when i do age = 14 it seems to work ok but it wont work with = gets
I dont understand any of that.
On Aug 16, 2013, at 6:30 PM, Chay H. [email protected] wrote:
Ok so i am trying to get my if statement to work but my console keeps
closing out
I’m curious, are you running your program by double-clicking it in a
file manager, such Windows Explorer or Nautalus? If so, what you are
experiencing has nothing to do with ruby or any other program you may be
running. It’s that the console opens to run the program and closes when
it quits.
Instead of doing that, open a terminal or command console separately,
and run your program at the command prompt.
If this is not what’s happening, please ignore.
If you are going to be a coder you need to learn to read code and docs
about code
I/O is short for Input/Output
‘gets’ is an input method
Is this case the docs are showing examples of how ‘gets’ can be called
in using just ‘gets’ with no parameters you are following the first
example
so the sep parameter gets the default value shown which is the special
sigil for End-of-Line aka EOL
which his the character you get when you hit enter
this " → string or nil " shows the value returned when ‘gets’ is called
either a string value or nil if there is nothing to get
Since its returning a string, comparing the value to an Integer will not
work (Ruby does not allow mixture of strings and ints like some langs)
As stu says convert it to an int, but remember to get rid of the EOL
char
first.
The consider what how can you handle if someone enters a non-numeric
chars?
There was also this thread Confusion with gets, if - Ruby - Ruby-Forum a
couple of months ago.
Looks like you need to convert your string into an integer.
age = gets
age = age.to_i
that should work.
On Sat, Aug 17, 2013 at 3:28 AM, Stu [email protected] wrote:
Looks like you need to convert your string into an integer.
age = gets
age = age.to_i
that should work.
OP may also want to do
age = Integer(gets)
This gives feedback if the entered string was not an valid integer
number
representation.
Kind regards
robert
Robert
I was at a ruby meetup this morning where the speaker was going over a
tutorial on testing. Through the refactoring steps it was serendipitous
much like this thread. At first they used to_i and at the cleanest point
of
the cycle which had the lowest token count while retaining bug/error
free
code by removing any complexity and overhead ended with Integer being
the
most optimum use.
I smiled and thought of this thread. I’ll be using Integer more often
now =)
~Stu
On Sat, Aug 17, 2013 at 4:34 AM, Robert K.
On Sat, Aug 17, 2013 at 8:10 PM, Stu [email protected] wrote:
=)
Funny, how things sometimes coincide.
Kind regards
robert