Ruby 数据类型

数据类型对您正在处理的数据种类进行分类,例如文本、数字和真/假值。例如,

num = 24

在这里,24(一个整数)被赋值给 num 变量。因此,num 的数据类型是 Integer

请注意,我们没有显式指定变量的数据类型。这是因为 Ruby 会自动推断变量的数据类型。这被称为动态类型


Ruby 数据类型

下表列出了一些常见的 Ruby 数据类型

数据类型 描述 示例
整数 不带小数点的整数。 5, -23, 1024
浮点数 带小数点的数字。 5.0, -23.85, 1024.369
字符串 包含在单引号或双引号中的文本值。 "Hello, World!", '25'
布尔型 两个值之一:truefalse true, false
数组 不同值的列表。 [1, 2, "hi"]
哈希 键值对的集合。 { name: "Ash" }
nil 表示“无”或“无值”。 nil
符号 轻量级标签/标识符。 :hello

Ruby 数字(整数和浮点数)

整数是没有小数点的数字。

同时,浮点数是有小数点的数字。

例如,

# Create and print an integer variable
marks = 85
puts marks

# Create and print a float variable
price = 99.99
puts price

输出

85
99.99

这里,

  • marks 是一个值为 85整数变量。
  • price 是一个值为 99.99浮点数变量。

要了解更多信息,请访问我们的 Ruby 数字 教程。


更多关于数字

算术运算

您可以在算术运算中使用数字。例如,

# Display the sum of 9 and 3
puts 9 + 3

# Display the product of 2.5 and 6.9
puts 2.5 * 6.9

输出

12
17.25
整数的算术运算

请始终记住,对整数进行算术运算的结果将始终是整数。例如,

puts 9 / 4

# Output: 2

在这里,94 都是整数。因此 9 / 4 将得到整数值 2,而不是实际结果 2.25

要解决此问题,您必须在运算中使用至少一个浮点数。例如,

# Divide 9.0 (float) by 4 (integer)
puts 9.0 / 4
# Output: 2.25

# Divide 9 (integer) by 4.0 (float)
puts 9 / 4.0
# Output: 2.25

# Divide 9.0 by 4.0
puts 9.0 / 4.0
# Output: 2.25

Ruby 字符串

字符串是引号中的一段文本。例如,

message = "Hello, I'm 29 years old."
puts message

age = '29'
puts age

输出

Hello, I'm 29 years old.
29

在这里,messageage 是字符串变量,因为它们存储了包含在引号中的字符序列。

注意:您可以使用单引号和双引号来表示字符串。但是,我们建议您使用双引号。

要了解更多信息,请访问我们的 Ruby 字符串 教程。

字符串与数字

请注意,在上面的程序中,age 变量存储的是字符串 '29'。即使我们理解这是一个数字,Ruby 也会将其解释为字符串,因为该值被引号括起来了。

# String value because of single quotes 
age_string = '29'
puts age_string

# String value because of single quotes
price_string = "99.99"
puts price_string

# Integer value (no quotations present)
age_int = 29
puts age_int

# Float value (no quotations present)
price_float = 99.99
puts price_float

输出

29
99.99
29
99.99

这里,

  • '29'"99.99" 是字符串,因为它们分别用单引号和双引号括起来了数字值。
  • 2999.99 是数字,因为它们没有被引号括起来。

注意: puts 方法在打印字符串时不会显示引号。因此,puts '29'puts 29 的输出都将是 29


Ruby 布尔值

布尔值可以是 truefalse。例如,

# Print boolean values
puts true
puts false

# Create boolean variables
can_vote = true
can_drink = false

# Print the boolean variables
puts can_vote
puts can_drink

输出

true
false
true
false

注意:布尔值用于条件语句和循环(我们将在后面的章节中学习)。

要了解更多信息,请访问 Ruby 布尔值


Ruby 数组

数组是值的列表。例如,

array_int = [1, 2, 3]
puts array_int

输出

1
2
3

在这里,array_int 是一个包含三个整数值:123 的数组。

您也可以创建其他类型的数组。您还可以将不同类型的数据混合到单个数组中。例如,

# An array of floats
array_float = [1.1, 2.2, 3.3]
p array_float

# An array of strings
array_string = ["Black Sabbath", "Deep Purple", "Led Zeppelin"]
p array_string

# Mixed array
array_mixed = ["Ash Ketchum", 10, false]
p array_mixed

输出

[1.1, 2.2, 3.3]
["Black Sabbath", "Deep Purple", "Led Zeppelin"]
["Ash Ketchum", 10, false]

提示:打印数组或其他复杂数据结构时,请使用 p 而不是 puts。它会显示完整的结构(包括引号、括号等)。

要了解更多信息,请访问我们的 Ruby 数组 教程。


更多关于数组

访问数组的单个元素

您可以使用索引访问单个数组元素,即表示元素位置的数值。

数组索引从 0 开始,这意味着

  • 0 表示第一个元素的位置。
  • 1 表示第二个元素的位置。
  • 以此类推。

例如,

rock_bands = ["Black Sabbath", "Deep Purple", "Led Zeppelin"]

# Display the first element
puts rock_bands[0]

# Display the second element
puts rock_bands[1]

# Display the third element
puts rock_bands[2]

输出

Black Sabbath
Deep Purple
Led Zeppelin

在这里,rock_bands 是一个数组,我们使用以下语法访问了该数组的每个元素

rock_bands[index]

因此,

  • rock_bands[0] 给出第一个元素,即 "Black Sabbath"
  • rock_bands[1] 给出第二个元素,即 "Deep Purple"
  • rock_bands[2] 给出第三个元素,即 "Led Zeppelin"

Ruby 哈希

哈希存储键值对。它类似于其他编程语言中的字典类型。例如,

# Create a hash named 'student'
student = {
  "id" => 121,
  "name" => "Ash Ketchum",
  "age" => 10
}

# Print the value stored in key "id"
puts student["id"]

# Print the value stored in key "name"
puts student["name"]

# Print the value stored in key "age"
puts student["age"]

输出

121
Ash Ketchum
10

在这里,student 是一个哈希,存储了以下键值对

"id" 121
"name" "Ash Ketchum"
"age" 10

请注意

  • 使用 => 符号将值赋给键。
  • 我们通过指定键来访问值。例如,student["id"] 给出存储在 student 哈希中的 "id" 键的值(121)。

要了解更多信息,请访问 Ruby 哈希


Ruby nil

Ruby 中的 nil 值表示“无”。这是 Ruby 表示“此处无值”的方式。

name = nil
puts name

此程序不打印任何内容,因为 nil 表示变量没有值。


Ruby 符号

符号是 Ruby 中一个轻量级的、不可变的标签。它常用于不需要完整字符串功能的场景,例如哈希中的键。

符号以冒号 (:) 开头。例如,

my_symbol = :hello
puts my_symbol

# Output: hello

在这里,my_symbol 是一个存储符号 :hello 的变量。

请注意,puts 方法在打印符号时不会显示冒号。

使用符号作为哈希键

使用符号作为哈希键

您也可以使用符号作为哈希键。例如,

student = {
  id: 121,
  name: "Ash Ketchum",
  age: 10
}

puts student[:id]
puts student[:name]
puts student[:age]

输出

121
Ash Ketchum
10

在这里,符号 :id:name:agestudent 哈希中的键。

请注意,我们在定义哈希时在标签末尾使用了冒号。

student = {
  id: 121,
  name: "Ash Ketchum",
  age: 10
}

但随后,我们在打印键值时在开头使用了冒号。

puts student[:id]
puts student[:name]
puts student[:age]

这是因为Ruby 1.9 引入了一种快捷语法,以便轻松地使用符号创建键值对。

旧的、不太方便的语法要求您以以下方式使用符号键

student = {
  :id => 121,
  :name => "Ash Ketchum",
  :age => 10
}

正如您所见,尽管新语法可能让初学者感到困惑,但它更加方便。


常见问题

如何查找变量或字面量的类?

您可以使用 .class 检查变量或字面量的类型。例如,

puts "hello".class
# Output: String

puts :hello.class
# Output: Symbol

puts 123.class
# Output: Integer

puts 12.3.class
# Output: Float

puts true.class
# Output: TrueClass

puts false.class
# Output: FalseClass

puts nil.class
# Output: NilClass

my_array = [1, 2, 3]
puts my_array.class
# Output: Array

my_hash = { name: "Ragnar Lothbrok" }
puts my_hash.class
# Output: Hash

请注意,Ruby 没有 Boolean 类。相反,truefalse 分别是 TrueClassFalseClass 的实例。

Ruby 中还有其他数据类型吗?

是的,Ruby 还支持其他几种数据类型,例如:

  • 范围:表示值的序列(例如,1..5)。
  • 复数:表示具有实部和虚部的数字。
  • 有理数:表示精确的分数(例如,1/2 而不是 0.5)。

让我们看一个复数和有理数的简单示例

require 'complex'
puts Complex(2, 3) # Output: 2+3i

puts Rational(1, 2) # Output: 1/2
你觉得这篇文章有帮助吗?

我们的高级学习平台,凭借十多年的经验和数千条反馈创建。

以前所未有的方式学习和提高您的编程技能。

试用 Programiz PRO
  • 交互式课程
  • 证书
  • AI 帮助
  • 2000+ 挑战