English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Ruby Data Types

In this chapter, we will introduce the basic data types of Ruby.

Ruby supports data types including basic Number, String, Ranges, Symbols, as well as the special values true, false, and nil, and also two important data structures - Array and Hash.

Number

Integer

There are two types of integer types, if in31bits (four bytes), that is an example of Fixnum. If it exceeds, it is an example of Bignum.

The integer range is from -230 to 230-1, integers in this range are of the class Fixnum object, when the integer value is greater than or equal to2of3to the power of 0 ( -262 to 262-1will be automatically converted to Bignum type.

You can use an optional leading symbol before an integer, an optional base indicator (0 corresponds to octal, 0x corresponds to hex, 0b corresponds to binary), followed by a sequence of digits. Underline characters are ignored in the digit string.

You can get the integer value of an ASCII character or an escape sequence marked with a question mark.

Online Examples

123                  # Fixnum Decimal
1_234                # Fixnum Decimal with an underscore
-500                 # Negative Fixnum
0377                 # Octal
0xff                 # Hexadecimal
0b1011               # Binary
"a".ord              # Character encoding of "a"
?\n                  # Encoding of newline (0x0a)
12345678901234567890 # Big number
 
#Integer Integer The following are some integer literals 
#Literals (literals): All values, numbers, bool values, strings, etc., that can be seen in the code are called literals 
#As in the following 0,1_000_000,0xa, etc. 
a1=0 
 
#Integer with thousands separator 
a2=1_000_000 
 
#Other representations of bases 
a3=0xa 
puts a1,a2 
puts a3 
 
#puts print are both used to print characters to the console, where puts returns a newline 
=begin 
This is a comment, known as: embedded documentation comments 
Similar to C#,/**/ 
=end

floating point type

Ruby supports floating-point numbers. They are numbers with decimals. Floating-point numbers are of the class Float of objects, and can be any of the following.

Online Examples

123.4                # Floating point value
1.0e6                # Scientific notation
4E20                 # Not required
4e+20                # Exponent sign before the exponent
 
#Floating point type 
f1=0.0 
f2=2.1 
f3=1000000.1 
puts f3

arithmetic operations

addition, subtraction, multiplication, and division operators:+-*/; the exponent operator is**

The exponent does not have to be an integer, for example

Online Examples

#Exponential arithmetic 
puts 2**(1/4)#1with4is 0, then2to the power of 0 is1 
puts 16**(1/4.0)#1with4.0 is 0.25(fourth root), then take the fourth root

string type

In simple terms, a Ruby string is a 8 bit sequences, which are objects of the String class.

Double-quoted strings allow replacement and use of backslash symbols, single-quoted strings do not allow replacement, and only the two backslash symbols \\ and \' are allowed.

Online Examples

#!/usr/bin/ruby -w
 
puts 'escape using \\\\
puts 'That\'s right';

This will produce the following result:

escape using "\\"
That's right

You can use a sequence #{ expr } Replace the value of any Ruby expression with a string. Here, expr can be any Ruby expression.

Online Examples

#!/usr/bin/ruby -w
 
puts "Multiply : #{24*60*60"

This will produce the following result:

Multiply : 86400

Online Examples

#!/usr/bin/ruby -w
 
name="Ruby" 
puts name 
puts "#{name+",ok""

The output is:

Ruby
Ruby,ok

Backslash symbols

The following table lists the backslash symbols supported by Ruby:

SymbolCharacter represented
\nNewline (0x0a)
\rCarriage return (0x0d)
\fPage break (0x0c)
\bBackspace key (0x08)
\aAlarm bell (0x07)
\eEscape character (0x1b)
\sSpace character (0x20)
\nnnOctal notation (n is 0-7)
\xnnHexadecimal notation (n is 0-9、a-f or A-F)
\cx, \C-xControl-x
\M-xMeta-x (c | 0x80)
\M-\C-xMeta-Control-x
\xCharacter x

For more details on Ruby strings, please see Ruby string (String).

Array

Array literals are defined within [] with commas separating elements, and support range definitions.

  • (1) Arrays are accessed through [] indexing

  • (2) Insert, delete, or replace elements through assignment operations

  • (3) through+Merge and delete elements, and the collection appears as a new collection

  • (4) Append elements to the original data using the << operator

  • (5) through*numbered array elements

  • (6) Union and intersection operations are performed using the | and & symbols (note the order)

Online Examples

#!/usr/bin/ruby
ary = [ "fred", 10, 3.14, "This is a string", "last element", ]
ary.each do |i|
    puts i
end

This will produce the following result:

fred
10
3.14
This is a string
last element

For more details on Ruby arrays, please see Ruby array (Array).

Hash type

Ruby hashes are a series of keys placed within curly braces/Pairs, separated by commas and =>. Trailing commas are ignored.

Online Examples

Online Examples

#!/usr/bin/ruby
 
hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh.each do |key, value|
    print key, " is ", value, "\n"
end

This will produce the following result:

red is 3840
green is 240
blue is 15

For more details about Ruby Hash, please see Ruby Hash (Hash).

Range Type

A range represents an interval.

Ranges are represented by setting a start value and an end value. Ranges can be constructed using s..e and s...e, or by Range.new.

Ranges are represented by setting a start value and an end value. Ranges can be constructed using s..e and s...e, or by Range.new. A range represents an interval. The range type is

range (1..5) means it includes the value 1, 2, 3, 4, 5, range (1...5) means it includes the value 1, 2, 3, 4 .

Online Examples

#!/usr/bin/ruby
 
(10..15).each do |n|
    print n, ' '
end

This will produce the following result:

10 11 12 13 14 15

For more details about Ruby Range, please see Ruby Range (Range).