Ruby 结构体类
Struct是一种紧凑的方式,使用访问器方法将一些属性组合在一起,而不需要创建一个明确的类。Struct类是一个特定类的创建者,每个类都被定义为持有一组变量和它们的访问器。Struct类的子类是 Struct::Tms
例子
# Ruby program to illustrate
# use of Struct
# creating Struct
# Geek is generated class
Geek = Struct.new(:tut_name, :cate_name) do
def gfg
"This is #{cate_name} class tutorial in #{tut_name}."
end
end
# creating object of struct
a = Geek.new("Ruby", "Struct")
puts a.gfg
输出
This is Struct class tutorial in Ruby.
类方法
new : 该方法创建了一个由字符串命名的新类,由给定符号的访问方法组成。如果省略名称字符串,那么将创建一个匿名结构类。否则,这个结构类的名称将作为Struct类中的常量出现,所以这个名称必须是系统中所有结构类中唯一的,并且应该以大写字母开头。当结构类被分配给常量时,它有效地将常量的名称赋予了该类。
Struct.new([string][, symbol])
Struct.new([string][, symbol]){block}
例子
# Ruby program to illustrate
# creating structure
# Creating a structure with a name in struct
Struct.new("Geek", :tutorial_name, :topic_name)
Struct::Geek.new("ruby", "Struct")
# Create a structure named by its constant
Geek = Struct.new(:tutorial_name, :topic_name)
p Geek.new("Ruby", "Struct")
输出
#<struct Geek tutorial_name="Ruby", topic_name="Struct">```
Struct.new类返回一个新的类对象,它被用来创建一个新结构的具体实例。在这个实例中,实际参数小于或等于为这个类定义的属性数量。未设置的参数的默认值是nil 。传递太多的参数将引发ArgumentError异常。
```ruby
Geek.new([obj])
例子
# Ruby program to illustrate
# creating objects of structure
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
p str.tutorial_name
p str.topic_name
输出
"Ruby"
"Struct"
实例方法
- == : 它被称为Equality。如果str与other_struct在实例变量的值上相等,它将返回true。而且它们必须是由Struct.new创建的同一类。否则,它将返回false。
str == other_struct
示例:
# Ruby program to illustrate
# check equality
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
other_struct = Geek.new("Java", "array")
str1 = Geek.new("Ruby", "Struct")
# Check equality
p str == other_struct
p str == str1
输出:
false
true
- [] : 它被称为Attribute Reference。它返回由符号或索引(0…length-1)命名的实例变量的值。如果命名的变量不存在,那么它将引发NameError,如果索引超出范围,那么它将引发IndexError。
str[symbol]
str[int]
示例:
# Ruby program to illustrate
# use of []
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
# Using []
p str[:tutorial_name]
p str["topic_name"]
输出:
"Ruby"
"Struct"
- []= : 它被称为 “属性赋值”。它用于将实例变量的名称与一个符号或obj的值用int进行分配,并将其返回。如果实例变量的名字不存在或者索引超出范围,那么就会引发NameError。
str[symbol] = obj
str[int] = obj
示例:
# Ruby program to illustrate
# use of []=
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
# Using []=
str[:tutorial_name]= "Java"
str[:topic_name]= "array"
p str.tutorial_name
p str.topic_name
输出:
"Java"
"array"
- each : 该方法为每个实例变量调用块,并将值作为参数传递。
str.each_pair{|obj| block}
示例:
# Ruby program to illustrate
# use of each method
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
# Using each method
str.each{|a| puts (a)}
输出:
Ruby
Struct
- each_pair : 该方法为每个实例变量调用块,并将名称和值作为参数传递。
str.each_pair{|symbol, obj| block}
示例:
# Ruby program to illustrate
# use of each_pair method
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
# Using each_pair method
str.each_pair{|tutorial_name, a| puts ("#{tutorial_name} => #{a}")}
输出:
tutorial_name => Ruby
topic_name => Struct
- length : 该方法返回实例变量的数量。该方法的返回类型是一个整数。
str.length
示例:
# Ruby program to illustrate
# use of length method
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
# Using the length method
p str.length
输出:
2
- members : 该方法返回一个字符串数组,代表实例变量的名称。
str.members
示例:
# Ruby program to illustrate
# use of members
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
# Using members method
p str.members
输出:
[:tutorial_name, :topic_name]
- size : 这个方法与Struct#length方法类似。这个方法的返回类型是一个整数。
str.size
- to_a : 该方法以数组形式返回该实例的值。
str.to_a
示例:
# Ruby program to illustrate
# use of to_a method
# Create structure
Geek = Struct.new(:tutorial_name, :topic_name)
# Creating objects
str = Geek.new("Ruby", "Struct")
# Using to_a method
p str.to_a[0]
p str.to_a[1]
输出:
"Ruby"
"Struct"
- values : 这个方法类似于Struct#to_a方法。
str.values
- values_at : 该方法返回一个数组,该数组由str中与给定索引对应的元素组成。选择器可以是整数指数或范围。
str.values_at([selector])
示例:
# Ruby program to illustrate
# use of value_at method
# Create structure
Geek = Struct.new(:p, :q, :r, :s)
# Creating objects
str = Geek.new(12, 13, 14, 15)
# Using values_at method
p str.values_at(2, 1)
p str.values_at(2, 1, 0, 3)
输出:
Geek
[14, 13]
[14, 13, 12, 15]
参考资料: https://ruby-doc.org/core-2.2.0/Struct.html