The example parses the value types and constants in Ruby AND the ruby numerical constants.
Number)
Integer)
There are two types of integer types. If the value is within 31 characters (four bytes), it is a Fixnum instance. If the value is exceeded, it is a Bignum instance.
The integer ranges from-230 to 230-1 or-262 to 262-1. Integers in this range are Fixnum-like objects, and integers out of this range are stored in Bignum-like objects.
You can use an optional leading symbol before the integer, an optional basic indicator (0 corresponds to octal, 0x corresponds to hex, 0b corresponds to binary), followed by a string of numbers. The underline character is ignored in the numeric string.
You can obtain the integer of an ASCII character or an escape sequence marked with a question mark.
Instance
123 # Fixnum decimal 1_234 # Fixnum underlined Decimal-500 # negative Fixnum0377 # octal 0xff # hexadecimal 0b1011 # binary "a". ord # "a" character encoding? \ N # linefeed (0x0a) encoding 12345678901234567890 # Bignum # Integer values below are some Integer literal values # literal values (literal): values that can be seen in the code, numerical values, bool values, strings and so on are called literally # For example, the following 0, 1 _ 000_000, a1 = 0 for 0xa # integer a2 = 0000000_000 with kilobytes # other hexadecimal representation a3 = 0xa puts a1 and a2 puts a3 # puts print characters to the console, here, puts carries a carriage return line break.
= Begin
This is an annotation called an embedded document annotation.
Similar to/**/in C /**/
= End
Floating Point Type
Ruby supports floating point numbers. They are decimal numbers. A floating point is a Float-like object and can be any of the following.
Instance
123.4 # floating point value 1.0e6 # scientific note 4E20 # Not required 4e + 20 # symbol before Exponent
# Floating Point f1 = 0.0 f2 = 2.1 f3 = 1000000.1 puts f3
Arithmetic Operations
Addition, subtraction, multiplication, and Division operators: +-*/; exponential operators **
The exponent does not have to be an integer, for example
# Exponential arithmetic puts 2 ** (1/4) # The quotient between 1 and 4 is 0, and then the 0 operator of 2 is 1 puts 16 ** (1/4. 0) # The vendors of 1 and 4.0 are 0.25 (1/4), and then use the quad-power root.
Ruby constant
A constant starts with an uppercase letter. Constants defined in a class or module can be accessed from inside a class or module, and constants defined outside a class or module can be accessed globally.
Constants cannot be defined in methods. Referencing an uninitialized constant produces an error. A warning is triggered when you assign values to initialized constants.
#! /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 }" endend # create object = Example. new () object. show
Here, VAR1 and VAR2 are constants. This produces the following results:
The value of the first constant is 100 the value of the second constant is 200