Help with determing if user input is a string or integer

Hi! I’m very new to this site so sorry if this is in the wrong place, as well new to coding in general. I have this project for a class (Honestly the last assignment I’m doing since I’ve decided coding is not for me so I’m switching majors.) where I am trying to create a journal or diary that accepts user input and allows them to search by index entry or by string. I have been successful in finding by index but getting to where I can determine if the user input is string or not is the problem. Here is my code so far.

puts 'Type in as many words as you\'d like. When you\'re finished, press enter on an empty line'
array = []
input = ' '
while input != ''
  input = gets.chomp
  array.push input
end

loop do
  puts "Great! If you wish to recall a previous entry please insert the number of it below! If you can not recall the number of the entry then please type Search"
  ans = gets.chomp.to_i
  if ans > 0 && ans <= array.length 
    puts "The element at position #{ans} is " + array[ans-1]
    break
     elsif ans < 0
    puts "You have to pick a number greater than 0!"
  if ans == "Search"
  puts 'What would you like to search for?'
  else
  puts 'That is not a valid search!'
  end 
  end
end

So the problem with the above code currently is that I made ans = gets.chomp.to_i therefore string input is made into an integer. I realize this but I am unsure how to code it to determine whether or not user input is string or integer and giving a different response based on that. Hoping to get something like this

if user input = string and user input == Search,
then put “Please type your search here!”

if user input = integer
puts "The element at position #{ans} is " + array[ans-1] (this is what I already have)

You can add another variable to check if the input is a string or integer.

check_input = input.integer?

if check input == true
puts “integer”
else
puts “string”
end

Note:
All input from gets is a string.

1. You can use the Kernel#Integer() method. For example:

Integer('hello') rescue 'Not Integer'
=> "Not Integer"

Integer('5') rescue 'Not Integer'
=> 5

Now combining this with STDIN#gets() in a file called
p.rb:

print 'Type a Number: '
puts Integer(STDIN.gets) rescue abort "You need to type a number \xF0\x9F\x98\x81"

Running:

bash [$] ruby p.rb
Type a Number: 5
5

bash [$] ruby p.rb 
Type a Number: hi
You need to type a number 😁

2. Another approach:

input = 'hello'
input.to_i.to_s == input
=> false

input = '50'
input.to_i.to_s == input
=> true

Let’s put that in a file called p.rb:

print 'Type a Number: '
puts STDIN.gets.tap(&:strip!).then { |x| x.to_i.to_s == x ? x.to_i : "Enter a number \xF0\x9F\x98\x92" }

Running:

bash [$] ruby p.rb
Type a Number: 5
5

bash [$] ruby p.rb 
Type a Number: hi
Enter a number 😒

Do note that sometimes STDIN.gets can return nil. For example if you run this on Atom Runner or Code Runner, it will not work. It’s a good idea to write STDIN.gets.to_s this will ensure you get a string, and this doesn’t create new string if STDIN.gets returns string, on the other hand, nil.to_s does create a new string.