I'm a beginner with Ruby, practicing looping and if statements.
I went a little off track with my course work, and tried to run the following code:
puts("Enter your age!")
age = gets.to_i
again = "Try again!"
while age <= 100
if age == 0
puts("You are very very young. " + again)
age = gets.to_i
elsif age >= 1 && age <= 5
puts("You are quite small. Though you are breathing, you may not understand this. " + again)
age = gets.to_i
else
puts("You are a capable of reading " + again)
age = gets.to_i
end
end
puts("Your age," + age + " is a good one")
When I run it, I am prompted to enter an age.
If the age I enter <= 100 everything works according to the code.
If not, I get the error:
rehearsal.rb:20:in +': no implicit conversion of Fixnum into String
(TypeError) from rehearsal.rb:20:in'
Other StackOverflow answers (solving this error message) I read suggested that the error was in misusing the "to_i" method. I made sure to include it, and my error is still here.
Where is my bug?
In Ruby, the + method for adding strings doesn't work on integers. In your example, the age variable is an integer. This might seem a little strange if you are used to working with JavaScript.
You need to use string interpolation
puts("Your age, #{age} is a good one")
This works.
Related
I want to get an output so that it adds one more year to an age input.
This is my code:
print "Age: "
age = gets.chomp.to_i
def age_next_year(int)
age.each do |int|
int += 1
end
puts "Next year I’ll be " + age_next_year(age)
I can't seem to get the correct output.
You don’t need to call each since it’s designed to iterate arrays and you have an integer. Also, you don’t need to reassign the value back to int local variable, just return it from the method:
def age_next_year(int)
int + 1
end
Another issue is you are trying to + what age_next_year method returns (integer) to the string. The explicit conversion is required there:
puts "Next year I’ll be " + age_next_year(age).to_s
I'm just asking, but why don't you do something like this:
print "Age: "
age = gets.chomp.to_i + 1
printf "Next year I'll be %d\n", age
# OR
puts "Next year I'll be " + age.to_s
It works fine for me, and it's smaller and quicker than what you did.
I have this loop:
puts "Welcome to the Loop Practice Problems"
puts " Write a number between 1 and 10, but not 5 or else...."
ans = gets.chomp!
if ans < 1
puts "Tf bruh bruh"
elsif ans > 10
puts "Now you just playin"
elsif x == 5
print "You wildin B"
else
puts "Fosho that's all I require"
end
It doesn't run properly, and I'm trying to understand why. If you can help me with this, I'd appreciate it.
If you know a good site for practice problems, I'd love to try it. I checked out Coderbyte and Code Kata, but the way they're set up doesn't look right, and they don't have questions to solve for fundamentals.
The issue here is that you're not converting ans to a number, but you're comparing it to one. ans is going to be a string.
In Ruby, when you compare a number to a string, Ruby says that the two aren't equal:
"1" == 1
=> false
You can reproduce the problem with this code:
puts "Welcome to the Loop Practice Problems"
puts " Write a number between 1 and 10, but not 5 or else...."
ans=gets.chomp!
p ans
The p method will output an "inspected" version of that object, (it's the same as doing puts ans.inspect). This will show it wrapped in quotes, which indicates that it's a string.
You probably want to do this:
ans = gets.chomp!.to_i
The to_i method here will convert the number to an integer, and then your comparisons will work correctly.
You have to convert input string type object into integer type
ans = gets.chomp!.to_i #input string convert into integer.
if ans < 1
puts "Tf bruh bruh"
elsif ans > 10
puts "Now you just playin"
elsif x == 5
print "You wildin B"
else
puts "Fosho that's all I require"
end
I'm trying to learn ruby, and was tasked with creating a simple program that grabs user input, and then uses an elsif statement to give a possible 3 different answers. Here's my code:
print "What is your birth year?"
year = gets.chomp
age = 2016 - year
if age >= 40
puts "You're old!"
elsif age.between?(25, 39)
puts "You aren't too old!"
else
puts "You're just a baby!"
end
However, I keep getting "String can't be coerced into Fixnum"
I don't recall in the lessons doing something like I'm doing for the "age" variable, so I could be doing it wrong, but I also couldn't find an answer in my lessons or online. Any help would be greatly appreciated!
Remember that gets returns a string value, and in Ruby strings and numeric values are completely different things. Some other languages will coerce them as necessary, like PHP, Perl or JavaScript, but Ruby does not.
To make this work:
year = gets.chomp.to_i
Although technically to_i alone is usually sufficient.
I wrote this program to ask users for their age, and then tell them how old they will be in 10-50 years. I don't understand what's wrong with it :( I'm just a beginner and any help would be greatly appreciated.
print "How old are you?"
age = gets.chomp
i = 1
while i < 6
multiple = i * 10 + age
puts "In #{multiple} years you will be #{multiple}"
i++
end
Many ways to accomplish what you're trying to do. Make sure you're indenting blocks correctly - it'll make your code much more readable. Note that to_i converts your input from a String to an Integer. Also, try to name your variables more specifically; multiple doesn't really mean anything in your example.
puts "How old are you?"
age = gets.chomp.to_i
(1..5).each do |i|
years_passed = i * 10
new_age = years_passed + age
puts "In #{years_passed} years you will be #{new_age}"
end
If you want to use a while loop, you could do:
puts "How old are you?"
age = gets.chomp.to_i
multiplier = 1
while multiplier <= 5
years_passed = multiplier * 10
new_age = years_passed + age
puts "In #{years_passed} years you will be #{new_age}"
multiplier += 1
end
I'm new to Ruby and am currently working on some practice code which looks like the following:
puts 'Hello there, Can you tell me your favourite number?'
num = gets.chomp
puts 'Your favourite number is ' + num + '?'
puts 'Well its not bad but ' + num * 10 + ' is literally 10 times better!'
This code however just puts ten copies of the num variable and doesn't actually multiply the number so I assume I need to make the 'num' variable an integer? I've had no success with this so can anyone show me where I'm going wrong please?
If you are using to_i, then chomp before that is redundant. So you can do:
puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts 'Your favourite number is ' + num.to_s + '?'
puts 'Well its not bad but ' + (num * 10).to_s + ' is literally 10 times better!'
But generally, using "#{}" is better since you do not have to care about to_s, and it runs faster, and is easier to see. The method String#+ is particularly very slow.
puts 'Hello there, Can you tell me your favourite number?'
num = gets.to_i
puts "Your favourite number is #{num}?"
puts "Well its not bad but #{num * 10} is literally 10 times better!"
Use the to_i method to convert it to an integer. In other words, change this:
num = gets.chomp
To this:
num = gets.chomp.to_i
you can also make sure the number that the user is using is an integer this way:
num = Integer(gets.chomp)
but you have to create a way to catch the error in case the user input otherwise like a char, or string so; it is must better to use:
num = gets.chomp.to_i
In case the user put another type of data, num will be equal to 0 like you can see in this test example:
puts "give me a number:"
num = gets.chomp.to_i
if num >3
puts "#{num} es mayor a 3 "
else
puts "#{num} es menor a 3 o 3"
end
This a example of the interaction with that script:
give me a number:
sggd
0 es menor a 3 o 3
nil
I hope this clarify better your point.
I wrote a similar program as yours. Here is how I finally got it to work properly! I had to assign the favorite number to be an integer. Then, in the next line I set the new_fav_num with the value of fav_num +1 and then converted it to string. After that, you can just plug your code into the return statement that you want to say to the user, only you have to convert the first fav_num to a string.
puts "What is your favorite number?"
fav_num = gets.chomp.to_i
new_fav_num = (fav_num + 1).to_s
puts "Your favorite number is " + fav_num.to_s + ". That's not bad, but " +
new_fav_num + " is bigger and better!"
Hope this helps.