Numeric Types2:32 with Jay McGavren
Programming languages use strings a lot, but they use numbers even more often. Numbers in Ruby can be used in all the math operations that other programming languages support. But unlike in most other languages, numbers in Ruby are also objects. That means they have lots of useful methods attached to them, just like with strings.
Here we have some code that assigns the integer
12 to a variable named
whole_number, and the fractional number
12.34 to a variable named
fractional_number. That dot within the numbers is important, because it decides which of Ruby's two most common numeric classes the number gets assigned to.
whole_number = 12 fractional_number = 12.34 p whole_number.class # Fixnum [Integer in Ruby 2.4 and later] p fractional_number # Float
- If we set
12.0, it'll still be treated as a
Float. The key is whether it contains a decimal place or not.
- The distinction between
Floatclasses is important, partly because it affects what methods are available on a number.
- If the number in your code has a decimal point, it's a
Float. Otherwise, it's a
You need to sign up for Treehouse in order to download course files.Sign up