红宝石 |结构类
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 中必须是唯一的,并且应以大写字母开头。当将结构化类分配给常量时,它实际上为类提供了常量的名称。
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.new 类返回一个新的类对象,用于创建新结构的特定实例。在这种情况下,实际参数小于或等于为此类定义的属性数。未设置参数的默认值为nil 。传递太多参数将引发ArgumentError异常。
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
- []:它被称为属性引用。它返回以符号命名的实例变量的值或以 int 命名的 index(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