Numbers. Computers think in numbers. At the machine level, numbers are represented as bits. Integers are bits. Floats are bits. But in Ruby we use higher-level numbers.
Ruby examples. We explore numeric types and conversions in this language. Conversions are done with the Integer and Float built-in methods.
Integer, Float. Here we convert string data to number types. A string may contain digits but not be a number. With the Integer conversion, we convert it to one. We also use Float.
Here: We convert the string "1234" into an Integer. And we convert a string with floating-point data into a Float.
Then: We add the two numbers together. This shows they are no longer strings, which could not be added in this way.
Based on: Ruby 2 Ruby program that converts strings, numbers # Two strings with numeric contents. value1 = "1234" value2 = "1234.5678" # Convert strings to numbers. number1 = Integer(value1) number2 = Float(value2) # Print numbers. print "Number1: ", number1, "\n" print "Number2: ", number2, "\n" # Add numbers together. # ... Could not be done with strings. number3 = number1 + number2 # Print final number. print "Number3: ", number3, "\n" Output Number1: 1234 Number2: 1234.5678 Number3: 2468.5678
Exponents. We can directly use exponents, with the two-star operator. In this program, we raise a value to the power of two, and then to the power of three.
Tip: More advanced mathematical operations are available as methods in the Math class.
Ruby program that uses exponents # An initial value. value = 3 # Square the value. square = value ** 2 # Cube the value. cube = value ** 3 # Display our results. puts square puts cube Output 9 27
Rand. This generates a pseudo-random number within in a range. The range is inclusive—the lowest or highest bounding numbers may be returned by rand. By default, a range of 0 to 1 is used.
Here: We generate a random number between 0 and 1. Then we generate six more, all between 0 and 5 (inclusively).
Srand: The srand method is also available: this seeds the random number generator. Srand is not normally needed.
Note: Calling srand with a constant number will make a program always use the same random numbers.
Ruby program that uses random numbers # Display random number between 0 and 1. puts rand # Display 6 random numbers between 0 and 5 inclusive. for i in 0..5 # Call with range. r = rand 0..5 puts r end Output 0.3299959902490742 2 4 2 0 1 5
Srand. This seeds the random number generator. Typically it is best not to invoke this method. It may make the random number stream more deterministic.
Note: If you use a constant with srand, the random stream of numbers is not random anymore. This does not seem random.
Instead: Each time I run this program, it returns 0.41 and 0.72. This is because the random number generator was seeded with a constant 1.
Ruby program that uses srand # Use constant seed. srand 1 # Display random numbers. puts rand puts rand Output 0.417022004702574 0.7203244934421581
Zero, nonzero. Zero() returns true or false. If the number is zero, it returns true. Otherwise, it returns false. Nonzero meanwhile returns the original number if it is not zero.
And: If the number is zero, nonzero returns nil. So it returns the number with 0 changed to nil.
Ruby program that uses zero, nonzero value = 0 if value.zero? puts "A" end if value.nonzero? puts "B" # Not reached. end value = 1 if value.nonzero? puts "C" end Output A C
Eql operator. Unlike the "==" operator, eql compares types. So a floating point number, like 1.0, is not equal to an Integer number like 1. The == operator treats them as equal.
Tip: Using the == operator is superior in most programs. The eql method just adds complexity—1.0 usually should equal 1.
Ruby program that uses eql value1 = 1 value2 = 1.0 value3 = 1 if value1.eql? value2 puts "A" # Not reached. end if value1 == value2 puts "B" end if value1.eql? value3 puts "C" end Output B C
Math. Some numeric operations can be done directly, with operators like + or minus. But many math methods exist, included for easy access. These handle more complex things like sqrt.Math
A summary. Tasks that involve numbers are often language-specific. In Ruby, we have many helpful operators available on numbers. We convert and manipulate numbers with ease.