Check if a string is a string in ruby - ruby

I want to check a string entered by a user. If it is a string, then move along, if not, then throw an error. I do not know how to check if the user input is a string or an int. Below is my code:
puts "what is your name?"
name = gets.chomp
if name == Int
puts "error enter a string"
end
if birthdate != Int
puts "error enter your birthdate"
end
puts "how old are you "
age = gets.to_i
if age == String
puts "error please enter your age"
end
puts "hello" + name + " wow that is a good day to be born" + "thats a great age"
puts "the half of your age is #{age/2.0} that is good to know"

It may be checked as this:
puts "what is your name?"
name = gets.chomp
if !(name =~ /[0-9]/).nil?
puts "error enter a string"
end
puts "what is your age?"
age = gets.chomp
if age.to_i.to_s == age
puts "error enter a integer"
end

I think this is what you are looking for.
if name.class == String
Also this will always be false
age = gets.to_i # this is converting to integer
if age.class == String # I think this is what you meant
age will always be an Integer her because you have cast it as such with .to_i

Related

What is wrong here in this Ruby Program?

I want in brief to run a program to check if the user input is empty to let him reinsert the needed data and in case there is "s" in the string to be substituted with another letter
print "Please enter a string: "
user_input = gets.chomp.downcase!
if user_input.empty?
print "Please enter a vaild string... "
user_input = gets.chomp.downcase!
elsif
user_input.include? "s"
user_input.gsub!(/s/, "th")
else
puts "There are no 's's in your string. #{user_input}"
end
puts "Your new thtring is #{user_input}."
The problem is with this line
user_input = gets.chomp.downcase!
according to the docs
Downcases the contents of str, returning nil if no changes were made.
So if the user inputs a string with only lowercase letters, nil is returned.
Your function works if a user enters FOO, then it works fine.
You're better off using downcase instead of downcase!. downcase always return the string itself.
As I understand you need get valid user input (with s)
Now you are only using if and this does not guarantee that user input will be valid
You can refactor to something like this
puts "Please enter a string with s:"
thtring = ""
loop do
user_input = gets.chomp
next puts "Please enter some string..." if user_input.empty?
thtring = user_input.downcase
next puts "There are no 's's in your string" unless thtring.include?("s")
break thtring.gsub!(/s/, "th")
end
puts "Your new thtring is #{thtring}."

Using regex in simple if statements?

Instead of doing:
puts "what type of input?"
input = gets.chomp
if %W[Int INT i I Ints ints].include?(input)
puts "enter int"
i = gets.to_i
I want to use regex to interpret string user input. For example,
puts "are you entering in a string, an int or a float?"
case gets
when /\A(string|s)\z/i
puts "enter in a string"
gets.chomp
when /\A(int|i)\z/i
puts "enter an int"
gets.to_i
when /\A(float|f)\z/i
puts "enter a float"
gets.to_f
end
What is the syntax in order to get the same result but using if statements instead of case statement?
gets returns a string with a trailing carriage return. What you need is to match the ending against \Z, not \z.
puts "are you entering in a string, an int or a float?"
case gets
when /\As(tring)?\Z/i
puts "enter in a string"
gets.chomp
when /\Ai(nt)?\Z/i
puts "enter an int"
gets.to_i
when /\Af(loat)?\z/i
puts "enter a float"
gets.to_f
else puts "Didn’t work"
end
I also slightly updated regexps to clearly show the intent.
If you want to turn your case into an if, you have to store the expression intended for the gets into a variable:
response=gets.chomp
if /..../ =~ response
...
elsif /.../ =~ response
....
....
else
...
end

Ruby won't divide string

I am trying to divide a user-input age by 2. My code is below:
puts "what is your name?"
name = gets.chomp
puts "when were you born please enter your birthdate"
birthdate = gets.chomp
puts "how old are you "
age = gets.chomp
puts "hello" + name + " wow that is a good day to be born" + "thats a great age"
puts "the half of your age is" + age/2 + " that is good to know"
It does not work.
Your age is a string
age = gets.to_i
Now it's a number. But you can't concatenate a string and a number. Two options:
interpolation
puts "the half of your age is #{age/2} that is good to know"
or
puts "the half of your age is " + (age/2).to_s + " that is good to know"

Using a method to puts variables

I want to use capitalize method and puts a string with the user input.
puts "What is your name?"
name = gets.chomp
puts "Hi, " name.capitalize "how are you?"
Here is the error after line 2:
syntax error, unexpected tIDENTIFIER, expecting $end puts "Hi, "
name.capitalize "how are you?"
You need to concatenate the strings or interpolate. Your options are:
puts "What is your name?"
name = gets.chomp
puts "Hi, " + name.capitalize + " how are you?"
or
puts "What is your name?"
name = gets.chomp
puts "Hi, #{name.capitalize} how are you?"
You need to use string interpolation. Here is a RubyMonk tutorial you may enjoy.
puts "Hi, #{name.capitalize}, how are you?"

How to re-prompt and re-use a user's input

I was trying to re-prompt a user's input and reuse it. Here's the code sample:
print "Please put your string here!"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/,"th")
elsif user_input.include? ""
user_input = gets.chomp
puts "You didn't enter anything!Please type in something."
user_input = gets.chomp
else
print "no \"S\" in the string"
end
puts "transformed string: #{user_input}!"
My elsif will let the user know that their input was not acceptable, but was not effective in re-using their input to start from the beginning. How am I supposed to do it? Should I use a while or for loop?
Hope this solves your problem :)
while true
print 'Please put your string here!'
user_input = gets.strip.downcase
case user_input
when ''
next
when /s/
user_input.gsub!(/s/, "th")
puts "transformed string: #{user_input}!"
break
else
puts "no \"S\" in the string"
break
end
end
You can have a loop at the beginning to continuously ask for input until it's valid.
while user_input.include? "" #not sure what this condition is meant to be, but I took it from your if block
user_input = gets.chomp
user_input.downcase!
end
This will continuously ask for input until user_input.include? "" returns false. This way, you don't have to validate input later.
However, I'm not sure what you are trying to do here. If you want to re-prompt when the input is empty, you can just use the condition user_input == "".
EDIT: Here's the doc for String.include?. I tried running .include? "" and I get true for both empty and non-empty input. This means that this will always evaluate to true.
user_input = nil
loop do
print "Please put your string here!"
user_input = gets.chomp
break if user_input.length>0
end
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/,"th")
else
puts "no \"S\" in the string"
end
puts "transformed string: #{user_input}!"

Resources