"would you like #{first + second} to be your new fav number?" how to add with interpolation? in ruby

To add two numbers with interpolation in a string in Ruby, you can use the following syntax:

main.rb
first = 5
second = 10

desired_number = "#{first + second}"

puts "Would you like #{desired_number} to be your new fav number?"
128 chars
7 lines

In this example, first and second are two variables which hold the numbers 5 and 10 respectively. By using string interpolation #{} within the string, we can perform the addition operation first + second and embed the result in the final string. The value of desired_number will be the sum of first and second, which is 15 in this case. Finally, the complete string with the interpolated value will be printed.

The output will be:

main.rb
Would you like 15 to be your new fav number?
45 chars
2 lines

Make sure to replace the values of first and second variables with your desired numbers.

related categories

gistlibby LogSnag