If statement not working with gets() in Ruby - ruby

When I ask the user for input like this:
puts "Enter num: "
num = gets()
if num == 44
puts "Yeah"
end
unless num == 44
puts "No"
end
the output is "No" when I enter 44 when prompted. I can't figure out what's going on, so please help.

The ouput is "No" because when you use gets actually the input is taken as string, so a mismatch occurs. So for your logic to work you should convert it into integer
So this is the solution
puts "Enter num: "
num = gets().to_i
if num == 44
puts "Yeah"
end
unless num == 44
puts "No"
end

You can simply do:
gets.to_i
which will essentially chomp the newline character and convert to integer.
Example:
2.1.2-perf :020 > num = gets.to_i
44
=> 44
2.1.2-perf :021 > if num == 44
2.1.2-perf :022?> puts "Yeah"
2.1.2-perf :023?> end
Yeah

gets() by itself included the newline character. Use gets.chomp to remove that extra character. Then, call .to_i to convert to an integer. So: gets.chomp.to_i.

Related

Chop last character of string

I want to make program which takes the string and chop last character each time and print result to console:
With an input string of Hello, the result should be:
Hello
Hell
Hel
He
H
This is my code so far:
def test_string
puts "Put your string in: "
string = gets.chomp
while string.length == 0
puts string.chop(/.$/)
end
end
puts test_string
Use chop!:
string = gets.chomp
# Print full string, e.g. "Hello"
puts string
# Print remaining... e.g. "Hell", "Hel", etc.
while string.length != 0
puts string.chop!
end
Following code does not modify the original string
string = gets.chomp
l = string.length
l.times do |i|
puts string[0..(l-i-1)]
end
You can also create an array filling it with the string N times, and for each time, get a character less from it:
str = 'Hello'
Array.new(str.size) { |index| str[0...str.size - index] }.each { |str| p str }
# "Hello"
# "Hell"
# "Hel"
# "He"
# "H

Is it possible to compare user input?

print "Enter your age in Numbers:"
user_input = gets.chomp
if user_input > 21 && user_input < 30
puts "XYZ"
elsif user_input > 31 && user_input < 40
puts "YZX"
elsif user_input > 40
puts "ZXY"
else
puts "Golden Age!"
end
The problem is that gets.chomp returns a string
You can convert it to integer by using String#to_i: gets.chomp.to_i
You can further improve the readability of your code by using between?:
print "Enter your age in Numbers:"
number = gets.chomp.to_i
if number.between?(22, 29)
puts "XYZ"
elsif number.between?(32, 39)
puts "YZX"
elsif number > 40
puts "ZXY"
else
puts "Golden Age!"
end
Or even better - a case statement:
print "Enter your age in Numbers:"
number = gets.chomp.to_i
case number
when (22...30) then puts "XYZ"
when (32...40) then puts "YXZ"
when (40..Float::INFINITY) then puts "ZXY"
else puts "Golden Age!"
end
Note: I made the ranges compatible with your original solution, but please note that there are some gaps in there (31 for example).
You can compare objects on which <=> is defined properly, but you cannot compare a string object with a fixnum object. If you intended to compare as an integer, you need to convert the input to integer. Also, (assuming that you only get inputs that can be converted to integers), your condition is redundant. And you are probably using && incorrectly. It can be written like this:
print "Enter your age in Numbers:"
user_input = gets.to_i
if user_input < 20 or [20, 21, 30, 31, 40].include?(user_input)
puts "Golden Age!"
elsif user_input < 30
puts "XYZ"
elsif user_input < 40
puts "YZX"
else
puts "ZXY"
end

How to check if user input is a number

I'm making this small program and I want to check if the user entered an Integer number, if he did it the program will continue but if the user enters a string I want to the program to ask the user to enter an Integer until he does it, here's my code snippet:
print "How old are you:"
user_age = gets.chomp.to_i
if user_age.is_a? String
puts "Please enter an integer number:"
user_age = gets.chomp.to_i
until user_age.is_a? Numeric
puts "Please enter an integer number:"
user_age = gets.chomp.to_i
break if user_age.is_a? Numeric
end
end
I think that your error is the to_i after gets.chomps.
to_i returns the first number(s) at the begining of a string or 0, so you allways get a number (0 or another number). Here are some examples:
2.2.1 :001 > "12".to_i
=> 12
2.2.1 :002 > "12aaa".to_i
=> 12
2.2.1 :003 > "aaa12aaa".to_i
=> 0
2.2.1 :004 > "aaaaaa".to_i
=> 0
I wrote this code, which works for me:
print "How old are you:"
begin
user_age = gets.chomp
user_age = Integer(user_age)
rescue ArgumentError
print "Please enter an integer number:"
retry
end
print user_age.to_s
begin
p "How old are you:"
user_age = Integer(gets.chomp)
rescue
p "Please enter an integer number:"
retry
end
print user_age
Maybe instead you could also use:
until user_age.to_i.to_s == user_age
puts "Please enter your age as a number"
user_age = gets.chomp
end
print "How old are you: "
user_age = gets.chomp
while true
if user_age.to_i.to_s == user_age
print "Your age is #{user_age}"
break
else
print "please enter your age in number "
user_age = gets.chomp
end
end

Need help validating if user input is a number

I have the following code:
def say(msg)
puts "=> #{msg}"
end
def do_math(num1, num2, operation)
case operation
when '+'
num1.to_i + num2.to_i
when '-'
num1.to_i - num2.to_i
when '*'
num1.to_i * num2.to_i
when '/'
num1.to_f / num2.to_f
end
end
say "Welcome to my calculator!"
run_calculator = 'yes'
while run_calculator == 'yes'
say "What's the first number?"
num1 = gets.chomp
say "What's the second number?"
num2 = gets.chomp
say "What would you like to do?"
say "Enter '+' for Addition, '-' for Subtraction, '*' for Multiplication, or '/' for Division"
operation = gets.chomp
if num2.to_f == 0 && operation == '/'
say "You cannot devide by 0, please enter another value!"
num2 = gets.chomp
else
result = do_math(num1, num2, operation)
end
say "#{num1} #{operation} #{num2} = #{result}"
say "Would you like to do another calculation? Yes / No?"
run_calculator = gets.chomp
if run_calculator.downcase == 'no'
say "Thanks for using my calculator!"
elsif run_calculator.downcase == 'yes'
run_calculator = 'yes'
else
until run_calculator.downcase == 'yes' || run_calculator.downcase == 'no'
say "Please enter yes or no!"
run_calculator = gets.chomp
end
end
end
I need it to take the num1 and num2 variables that the user inputs and validate that they are numbers and return a message if they aren't.
I would like to use a Regex, but I don't know if I should create a method for this or just wrap it in a loop.
The Integer method will raise an exception when the given string is not a valid number, whereas to_i will fail silently (which I think is not desired behavior):
begin
num = Integer gets.chomp
rescue ArgumentError
say "Invalid number!"
end
If you want a regex solution, this will also work (although I recommend the method above):
num = gets.chomp
unless num =~ /^\d+$/
say "Invalid number!"
end
You would often see each section written something like this:
ERR_MSG = "You must enter a non-negative integer"
def enter_non_negative_integer(instruction, error_msg)
loop do
puts instruction
str = gets.strip
return str.to_i if str =~ /^\d+$/
puts error_msg
end
end
x1 = enter_non_negative_integer("What's the first number?", ERR_MSG)
x2 = enter_non_negative_integer("What's the second number?", ERR_MSG)
Here's possible dialog:
What's the first number?
: cat
You must enter a non-negative integer
What's the first number?
: 4cat
You must enter a non-negative integer
What's the first number?
: 22
#=> 22
What's the second number?
: 51
#=> 51
x1 #=> 22
x2 #=> 51

Regular expression to match number formats

I'm trying to write a small program that asks a user to input a number and the program will determine if it's a valid number. The user can enter any kind of number (integer, float, scientific notation, etc.).
My question is what regular expression should be used to keep something like "7w" from being matched as a valid number? Also I would like the number 0 to exit the loop and end the program, but as it's written now 0 is matching valid like any other number. Any insight please?
x = 1
while x != 0
puts "Enter a string"
num = gets
if num.match(/\d+/)
puts "Valid"
else
puts "invalid"
end
if num == 0
puts "End of program"
x = num
end
end
You need to execute this script from the command line, not a text editor, as you will be prompt to enter a number.
This is the ultra compact version
def validate(n)
if (Float(n) != nil rescue return :invalid)
return :zero if n.to_f == 0
return :valid
end
end
print "Please enter a number: "
puts "#{num = gets.strip} is #{validate(num)}"
Output:
Please enter a number: 00
00 is zero
Here is a longer version that problably you can extend it to tweak it to your needs.
class String
def numeric?
Float(self) != nil rescue false
end
end
def validate(n)
if n.numeric?
return :zero if n.to_f == 0
return :valid
else
return :invalid
end
end
print "Please enter a number: "
puts "#{num = gets.strip} is #{validate(num)}"
And here is a test for all the possible cases
test_cases = [
"33", #int pos
"+33", #int pos
"-33", #int neg
"1.22", #float pos
"-1.22", #float neg
"10e5", #scientific notation
"X", #STRING not numeric
"", #empty string
"0", #zero
"+0", #zero
"-0", #zero
"0.00000", #zero
"+0.00000", #zero
"-0.00000", #zero
"-999999999999999999999995444444444444444444444444444444444444434567890.99", #big num
"-1.2.3", #version number
" 9 ", #trailing spaces
]
puts "\n** Test cases **"
test_cases.each do |n|
puts "#{n} is #{validate(n)}"
end
Which outputs:
Please enter a number: 12.34
12.34 is valid
** Test cases ** 33 is valid
+33 is valid
-33 is valid
1.22 is valid
-1.22 is valid
10e5 is valid
X is invalid
is invalid
0 is zero
+0 is zero
-0 is zero
0.00000 is zero
+0.00000 is zero
-0.00000 is zero
-999999999999999999999995444444444444444444444444444444444444434567890.99 is valid
-1.2.3 is invalid
9 is valid
Source for the idea of checking if its numeric:
How can I see if the string is numeric?
http://mentalized.net/journal/2011/04/14/ruby_how_to_check_if_a_string_is_numeric/
To start with, you are overcomplicating the loop, secondly, your regular expression needs to be a bit more explicit. Try something like this:
x = 1
while x != 0
puts "Enter a string"
num = gets.chomp
if num == "0"
puts "exiting with 0"
exit 0
elsif num.match(/^[0-9](\.[0-9])?$+/)
puts "Valid"
else
puts "invalid"
end
end
This expression will match any number that contains a digit or a decimal, and will fail to match anything containing letters. If the string entered is exactly 0, the script will exit with status 0.

Resources