How do I add one more year to the given input? - ruby

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.

Related

Ruby Error: "no implicit conversion of Fixnum into String"

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.

I don't understand what's wrong with this simple Ruby code

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

Ruby - Random Number is the Same Random Number, Every Time

wordList = ['1930', '1931', '1932', '1933', '1934', '1935', '1936', '1937', '1938', '1939', '1940']
wordLen = wordList.length
wordRand = rand(wordLen)
year = wordList[wordRand]
Very much a newb here... The behavior of year is such that every time I run the program, it selects a random string from wordList. The problem is that it takes that particular randomly-selected number and sets it as equal to year. So, for every instance of the program, year is the same string from the list each time I call it. How can I get it to select a different number each time?
puts 'Why hello there, dear! Grandma is, SO, happy to see you!'
response = gets.chomp
while response != 'BYE'
if response == response.upcase
puts 'NO, NOT SINCE ' + year + '!'
response = gets.chomp
else
puts 'WHAT? SPEAK UP, SONNY!'
response = gets.chomp
end
end
if response == 'BYE'
puts 'OKAY, BYE DEAR!!'
end
edit: added context
You appear to only be generating one value for year, then repeatedly using it in your loop. If you want different numbers, put the call to rand within the loop.

How to display display numeric value as string in ruby?

def sumof
s = 12 + 17
puts "The sum of 12 and 17 is: " + s
end
When I call sumof, I'm getting an error
Thanks for helping
There is really one way to convert to a string, but it can be used in multiple ways. The method is called to_s (to string).
Way 1 (manual):
"Some string " + num.to_s
Way 2 (interpolation):
"Some string #{num}"
You're going to want to do
"The sum is " + s.to_s OR "The sum is #{s}"
the problem being that conversion to string is not implicitly done in your original example.

Ruby gets/puts only for strings?

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.

Resources