More Arithmetic

The other two arithmetic methods are ** (exponentiation) and % (modulus). So if you want to say “five squared” in Ruby, you would write it as 5**2. You can also use floats for your exponent, so if you want the square root of 5, you could write 5**0.5. The modulus method gives you the remainder after division by a number. So, for example, if you divide 7 by 3, you get 2 with a remainder of 1. Let’s see it working in a program:

puts 5**2
puts 5**0.5
puts 7/3
puts 7%3
puts 365%7
25
2.23606797749979
2
1
1

From that last line, we learn that a (nonleap) year has some number of weeks, plus one day. So if your birthday was on a Tuesday this year, it will be on a Wednesday next year. You can also use floats with the modulus method. Basically, it works the only sensible way it could…but I’ll let you play around with that.

I have one last method to mention before we check out the random number generator: abs. This method simply returns the absolute value of the number:

puts (5-2).abs
puts (2-5).abs
3
3
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.156.22