Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
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
fractional_number
to12.0
, it'll still be treated as aFloat
. The key is whether it contains a decimal place or not. - The distinction between
Fixnum
/Integer
andFloat
classes 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 aFixnum
/Integer
.
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up