minus Ruby data type

Ruby data type

In this chapter we will introduce you to Ruby's basic data types.

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

Value type (Number)

1, Integer

There are two types of integers. If it is within 31 bits (four bytes), it is a Fixnum instance. If it is exceeded, it is the Bignum instance.

Integer ranges from -230 to 230-1, and integers in this range are objects of class Fixnum, when integers When the value is greater than or equal to 2, the 30th power ( -262 to 262-1) will automatically be converted to Bignum type.

You can use an optional preamble before the 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 ASCII character or an integer value of an escape sequence marked with a question mark.


123 # Fixnum Decimal 1_234 # Fixnum Underlined decimal -500 # minusFixnum 0377 # Octal 0xff # Hexadecimal 0b1011 # Binary "a".ord # "a" Character encoding ?\n # Line break (0x0a) encoding 12345678901234567890 # Large number #Integer Integer Here are some integer literals #Literal: The values, values, bool values, strings, etc. that can be seen in the code are called literals. #Such as 0,1_000_000, 0xa, etc. below a1=0 #Integer with a thousandth a2=1_000_000 #Other hexadecimal representation a3=0xa puts a1,a2 puts a3 #puts print Both print characters to the console, where puts takes a carriage return line feed =begin This is a comment called: Embedded Document Comments similarC#Medium / ** / =end

Floating point type

Ruby supports floating point numbers. They are numbers with decimals. A floating point number is an object of class Float and can be any of the following.



123.4 # Floating point value 1.0e6 # Scientific notation 4E20 # Not required 4e+20 # Symbols before the index #floating point f1=0.0 f2=2.1 f3=1000000.1 puts f3

Arithmetic operation

Addition, subtraction, multiplication and division operators: +-*/; index operator is **

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


#index arithmetic puts 2**(1/4) The quotient of #1 and 4 is 0, then the 0th power of 2 is 1 Puts 16**(1/4.0 )#The quotient of 1 and 4.0 is 0.25 (one quarter), then four roots

String type

A Ruby string is simply an 8-bit byte sequence, which is an object of class String .

Double quoted strings allow the replacement and use of backslashes, single quoted strings are not allowed to be replaced, and only the \\ and \' two backslashes are allowed.


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

This will produce the following results:

escape using "\"
That's right

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


#!/usr/bin/ruby -w puts "Multiply: #{24*60*60}";< /span>

This will produce the following results:

Multiply : 86400


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

The output is:


backslash symbol

The following table lists the backslashes supported by Ruby:

symbol represents the character
\nNewline (0x0a)
\rCarriage Return (0x0d)
\fpage breaks (0x0c)
\bBackspace (0x08)
\aAlarms Bell (0x07)
\eescape (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)
\xcharacter x


Array literals are defined by commas in [] and support range definitions.

  • (1) Array access via [] index
  • (2) Insert, delete, replace elements by assignment operation
  • (3) Combine and delete elements by +, -, and the collection appears as a new collection
  • (4) Append elements to the original data by <<#
  • (5) Repeat array elements by * sign
  • (6) Do the union and intersection operations with the | and & symbols (note the order)


#!/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 results:

This is a string
last element

For more details on Ruby arrays, check out Ruby Arrays (Array).

Hash type


Ruby hash is a set of key/value pairs placed inside the braces, separated by a comma and sequence =>. The comma at the end is ignored.



#!/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 results:

red is 3840
green is 240
blue is 15

Scope type

A range speaks to an interim. The

run is spoken to by setting a begin esteem and an end esteem. Reaches can be built utilizing s..e and s...e or developed by Range.new.

Use .. The extent of the develop keeps running from the begin an incentive to the end esteem (counting the end esteem). The range developed with ... keeps running from the begin an incentive to the end esteem (without the end esteem). At the point when utilized as an iterator, the range restores each an incentive in the grouping.

Range (1..5) implies that it contains the qualities ​​1, 2, 3, 4, 5, and the range (1...5) implies that it contains the qualities ​​1, 2, 3, 4 .


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

This will produce the following results:

10 11 12 13 14 15

welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.