Chapter 4

Other Native Objects

Now that we’ve taken a look at strings and arrays, we’ll continue with a tour of some other important Ruby objects: math, dates, regular expressions, and hashes.

4.1 Math

Like most programming languages, Ruby supports a large number of mathematical operations:

Be especially careful with division, though; it can be counter-intuitive:

Here Ruby uses integer division, which returns the number of times the denominator goes into the numerator. When dividing one number by another, chances are you want floating-point division instead, which you can get by adding a “point zero” to at least one of the numbers:

Get Learn Enough Ruby to Be Dangerous: Write Programs, Publish Gems, and Develop Sinatra Web Apps with Ruby now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.