Number type (Number)
Integer
There are two types of integers. If they are within 31 bits (four bytes), then they are Fixnum instances. If it exceeds it is a Bignum instance.
Integers range from -230 to 230-1 or -262 to 262-1. Integers in this range are objects of the class Fixnum, and integers outside this range are stored in objects of the class Bignum.
You can use an optional leading symbol before an integer, an optional base indicator (0 for octal, 0x for hex, 0b for binary), followed by a string of numbers. Underscore characters are ignored in numeric strings.
You can get an integer value of an ASCII character or an escaped sequence marked with a question mark.
Example
123 # Fixnum Decimal1_234 # Fixnum Decimal with underscore-500 # Negative Fixnum0377 # octal0xff # hexadecimal0b1011 # Binary"a".ord # "a" character encoding?\n # Encoding of newline characters (0x0a)12345678901234567890 # Bignum #Integer Integer The following are some integer literals#literal: The values, values, bool values, strings, etc. that can be seen in the code are all called literals#As shown in the following 0, 1_000_000, 0xa, etc.a1=0 #Integer with thousand marka2=1_000_000 #Other Category Representationa3=0xa puts a1,a2 puts a3 #puts print All print characters to the console,inputsBring carriage return line break
=begin
This is a comment,Called:Embedded document comments
similarC#In-house/**/
=end
Floating point type
Ruby supports floating point numbers. They are numbers with decimals. A floating point number is an object of the class Float and can be any of the following.
Example
123.4 # Floating point value1.0e6 # Scientific notation4E20 # Not required4e+20 # Symbols before the index
#Floating point typef1=0.0 f2=2.1 f3=1000000.1 puts f3
Arithmetic operation
Addition, subtraction, multiplication and division operator: +-*/; exponent operator is **
The index does not have to be an integer, e.g.
#Exponential Arithmeticputs 2**(1/4)The quotient of #1 and 4 is 0, and then the power of 0 of 2 is 1puts 16**(1/4.0)#1and4.0The business of0.25(One quarter),Then the fourth power root
Ruby Constants
Constants start with capital letters. Constants defined within a class or module can be accessed from within a class or module, and constants defined outside a class or module can be accessed globally.
Constants cannot be defined within methods. Referring to an uninitialized constant will produce an error. Assigning values to constants that have been initialized will generate a warning.
#!/usr/bin/ruby # -*- coding: UTF-8 -*- class Example VAR1 = 100 VAR2 = 200 def show puts "The value of the first constant is #{VAR1}" puts "The value of the second constant is #{VAR2}" end end # Create an objectobject=()
Here, VAR1 and VAR2 are constants. This will produce the following results:
The value of the first constant is 100 The value of the second constant is 200