Ruby has 2 classes from which integers can be instantiated from:
AnnouncementI have released my new course on Udemy, Kubernetes By Example. Sign up now to get free lifetime access!
Both of these classes are derived from the class called “Integer”. I.e. they are child class of Integer.
The range of number that Fixnum supports depends on your machine’s setting and version of Ruby installed. whereas Bignum is limited by the amount of ram you have on your machine. Here is the boundary on my machine:
puts 1070000000.class puts 1080000000.class
PS C:\Temp\irb> ruby .\numbers.rb Fixnum Bignum PS C:\Temp\irb>
Ruby handles the conversions of these two types automatically.
Floating numbers, on the otherhand are instantiated from the “Float” class, e.g.:
puts 10.5321345 puts 10.5e3 puts 10.5321345.class puts 10.5e3.class
PS C:\Temp\irb> ruby .\numbers.rb 10.5321345 10500.0 Float Float PS C:\Temp\irb>
Note: you need to have one digit before a the decimal point, and one after.
Note, objects that hold numbers, are set to read-only. Becuase of this, increment/decrement notations like i++ and i– don’t exist in ruby. Instead the closest thing to this that you have is:
a = 1 puts a a = a + 1 puts a a += 1 # shorthand way of writing a = a +1 puts a # a++ # this won't work and will output error message.
PS C:\Temp\irb> ruby .\numbers.rb 1 2 3 PS C:\Temp\irb>