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"
Related
This is a program that ask input from the user about the characteristics of a gemstone then prints to the screen. Things like color, price and name. I have written it to an extend where the user enters all this and prints them out. Am now stuck where I should loop and enable the user to enter any number of gemstones. Like if he/she enters 3 gemstones then it should loop and allow the user to enter the characteristics of the 3 gemstone types. I would also like to sort the resulting output of gemstone names in alphabetical order. Appreciated
class GemStones
# input variables
name = ""
color = ""
price = 0
gemstoneNumber = 0
# output variable
gemstoneNumber = 0
# processing
print "How many gemstones do you want to enter? "
gemstoneNumber = gets
print "What is the name of the gemstone? "
name = gets
print "What is the color of the gemstone? "
color = gets
print "What is the price of the gemstone? "
price = gets
puts " You entered #{gemstoneNumber} The name is #{name}, the color is #{color} and price is
$ #{price}"
end
You should not wrap the code in class in the first place. There is no OOP in your code, hence the class is not needed as well. Also, gets returns a string, while for number you likely need an integer.
Here would be a [more-or-less] rubyish version of your code:
print "How many gemstones do you want to enter? "
# ⇓⇓⇓⇓⇓ get rid of trailing CR/LF
# ⇓⇓⇓⇓ convert to integer
gemstoneNumber = gets.chomp.to_i
gemstones =
1.upto(gemstoneNumber).map do |i|
puts
puts "Please enter data for the gemstone ##{i}:"
print "What is the name of the gemstone? "
name = gets.chomp # shave the trailing CR/LF off
print "What is the color of the gemstone? "
color = gets.chomp
print "What is the price of the gemstone? "
price = gets.chomp.to_f # convert to float
# in Ruby we normally use hashes to store
# the named values
{name: name, color: color, price: price}
end
puts "You entered #{gemstoneNumber} gemstones. They are:"
gemstones.each do |gemstone|
puts "Name: #{gemstone[:name]}. " \
"Color: #{gemstone[:color]}. " \
"Price: $#{gemstone[:price]}."
end
Alternatively, you might use the class instead of hash to store the gemstone info.
To sort the gemstones by the name:
puts "You entered #{gemstoneNumber} gemstones. They are:"
# ⇓⇓⇓⇓⇓⇓⇓ HERE
gemstones.sort_by { |gemstone| gemstone[:name] }.each do |gemstone|
puts "Name: #{gemstone[:name]}. " \
"Color: #{gemstone[:color]}. " \
"Price: $#{gemstone[:price]}."
end
The good documentation on enumerations might be found in the official ruby docs: https://ruby-doc.org/core/Enumerable.html#method-i-sort_by (and around.)
You can try this as well.
def gem_stones num
#gem_stones = []
num.times do |a|
print "Enter the name of gemstone #{a+1} "
name=gets.chomp
print "Enter the color of gemstone #{a+1} "
color = gets.chomp
print "Enter the price of gemstone #{a+1} "
price = gets.chomp.to_f
#gem_stones.push({name: name, color: color, price: price})
end
puts #gem_stones.sort_by {|a| a[:name]}.map{|gem| "Name: #{gem[:name]}, Color: #{gem[:color]}, Price: #{gem[:price]}"}.join("\n")
end
puts "Ener the number of gem stones you want to enter?"
num = gets.to_i
gem_stones num
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
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 can I take input from keyboard with Ruby?
Try this:
puts "In which city do you stay?"
STDOUT.flush
city = gets.chomp
puts "The city is " + city
I'm making a simple Ruby program to add the letters of a person's full name, but I don't know how to add the variables in the code.
puts "What's your first name?"
first = gets.chomp
puts "What's your middle name?"
middle = gets.chomp
puts "What's your last name?"
last = gets.chomp
puts "You have " + first.length.to_s + middle.length.to_s + last.length.to_s + " letters in your name."
If I put a name like "John Jacob Smith", I get "You have 455 letters in your name" instead of "You have 14 letters in your name".
puts "You have " + first.length.to_s + middle.length.to_s + last.length.to_s + " letters in your name."
first.length is 4 (a number), because "john" has 4 letters.
first.length.to_s is "4" (a string) because you turned the number into as string - too early.
In the rest of your code, you 'add' two other strings to it, to get "455"
4 + 5 = 9 # what you want
"4" + "5" = "45" # what you got
puts "You have " + (first.size + middle.size + last.size).to_s + " letters in your name."
# or
puts "You have #{first.size + middle.size + last.size} letters in your name."
or little more "Rubyish"
puts "You have " + [first, middle, last].inject(0){|s,w| s+=w.size}.to_s + " letters in your name."
Try this: puts "You have " + (first.length + middle.length + last.length) + " letters in your name."
That's because you're adding actual string representations of numbers instead of numbers thembselves. Check the #fl00r's answer