Ruby |结构类

结构 是一种使用访问器方法将多个属性组合在一起的紧凑方式,无需创建显式类。这个 结构类 是特定类的创建者,每个类都定义为包含一组变量及其访问器。的子类 结构 班级是 结构:Tms .

null

例子:

# 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.

类方法

新的: 此方法创建一个名为string的新类,该类由给定符号的访问器方法组成。如果省略名称字符串,则将创建匿名结构类。否则,该结构的名称将在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">

结构。new class返回一个新的class对象,用于创建新结构的特定实例。在本例中,实际参数小于或等于为此类定义的属性数。未设置参数的默认值为 .传递太多参数将引发 论证错误 例外

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"

实例方法

  1. == : 它被称为 平等 .如果 str 等于 其他结构 根据实例变量的值。而且它们必须与 结构。新 .否则,它将返回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
    
  2. [] : 它被称为 属性引用 。它返回以符号命名的实例变量的值或以int命名的索引(0..length-1)。如果命名变量不存在,则会引发 命名错误 如果指数超出范围,那么它就会上升 索引器 .
    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"
    
  3. []= : 它被称为 属性分配 .它用于为实例变量名分配符号或 obj 按int并返回它。如果实例变量的名称不存在或索引超出范围,则会引发 命名错误 .
    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"
    
  4. 每个: 此方法为每个实例变量调用块,并将值作为参数传递。
    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
    
  5. 每对: 该方法为每个实例变量调用block,并将名称和值作为参数传递。
    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
    
  6. 长度: 此方法返回实例变量的数量。此方法的返回类型为整数。
    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
    
  7. 成员: 此方法返回表示实例变量名称的字符串数组。
    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]
    
  8. 尺寸: 这种方法类似于 结构#长度 方法此方法的返回类型为整数。
    str.size
  9. 致_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"
    
  10. 价值观: 这种方法类似于 结构#到#a 方法
    str.values
  11. 价值观在: 此方法返回一个数组,其中包含 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

© 版权声明
THE END
喜欢就支持一下吧
点赞15 分享