FAQ: Introduction to Ruby - Math

This community-built FAQ covers the “Math” exercise from the lesson “Introduction to Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Math

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi there,

I’m trying to do the simple maths equations but the result is not appearing, for example I inputted:

1+1

Please can someone help!

2 Likes

you just told ruby to do math, but not to display the result of this math operation. Maybe go to the next lesson? The next lesson covers puts and print

3 Likes

What is supposed to be wrong in the above program?

you want to get the user input and store it in the variable. which has the following syntax:

a = gets

what you did, is trying to use an undefined variable (a)

1 Like

print "give me a number: "
a=gets.to_i
print "another one: "
b=gets.to_i
puts “the addition is: #{a+b}”
puts “the sutraction is: #{a-b}”
puts “the multiplication is: #{a*b}”
puts “the division is: #{a/b}”

Does anybody know how to get the code to display in the compiler on exercise 7/16? I put
puts
“I love espresso”.length

But I get

wrong number of arguments (given 0, expected 1)

Hello, @method6003522183! Welcome to the forum!

In the code editor is your code on two separate lines as shown in your post? If so, that is the issue. Should be on one line like this:

puts "I love espresso".length

I’m playing with the cube root syntax Math.cbrt(num). What’s interesting is that when calculating the cube root of some numbers, Ruby does not always return a value as expected.

a = 2**3
puts Math.cbrt(a) #=> 2.0

a = 3**3
puts Math.cbrt(a) #=> 3.0000000000000004

a = 4**3
puts Math.cbrt(a) #=> 4.0

a = 6**3
puts Math.cbrt(a) #=> 6.000000000000001

My question is twofold:

  1. Why does Ruby sometimes return an accurate cube root, but sometimes it returns a long, approximated float number even when we can find an integer cube root in math?
  2. Is there any way to force Ruby return an accurate cube root, in cases like a = 3**3 and a = 6**3?

Although this is for python:

https://docs.python.org/2/tutorial/floatingpoint.html

most of these issues apply to ruby, certain numbers are difficult for the computer because of binary representation.

2 Likes

Perhaps this lesson should check for the use of the mathematical operators +, -, *, **, /, and/or % at least once?

It appears that you just have to hit the Run button (or write any code) and then hit Next. The code I tested was:

hello = "hello"
puts hello

It produced the expected result but used no math at all. You could obviously argue that it is the learner’s loss for not practicing the math, but you could argue the same thing for every other lesson. For example, the following lesson on puts and print requires you to use each at least once.

have you tried googling something like: ruby print vs put?