Ruby | Struct Class
Last Updated :
18 Sep, 2019
Struct is a compact way to group together a number of attributes, using accessor methods, without creating an explicit class. The Struct class is a creator of specific classes, each one is defined to hold a set of variable and their accessors. The subclass of Struct class is Struct::Tms.
Example:
Geek = Struct. new ( :tut_name , :cate_name ) do
def gfg
"This is #{cate_name} class tutorial in #{tut_name}."
end
end
a = Geek. new ( "Ruby" , "Struct" )
puts a.gfg
|
Output:
This is Struct class tutorial in Ruby.
Class Method
new : This method creates a new class named by string, consisting accessor methods for the given symbols. If the name string is omitted, then the anonymous structure class will be created. Otherwise, the name of this struct will appear as a constant in Struct class, so the name must be unique from all the Structs in the system and should start with capital letters. When a structured class is assigned to the constant, it effectively gives the class the name of the constant.
Struct.new([string][, symbol])
Struct.new([string][, symbol]){block}
Example:
Struct. new ( "Geek" , :tutorial_name , :topic_name )
Struct::Geek. new ( "ruby" , "Struct" )
Geek = Struct. new ( :tutorial_name , :topic_name )
p Geek. new ( "Ruby" , "Struct" )
|
Output:
#<struct Geek tutorial_name="Ruby", topic_name="Struct">
Struct.new class return a new class object, which is used to create a specific instance of the new structure. In this instance, the actual parameter is less than or equal to the number of attributes defined for this class. The default value of unset parameters is nil. Passing of too many parameters will raise an ArgumentError exception.
Geek.new([obj])
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
p str.tutorial_name
p str.topic_name
|
Output:
"Ruby"
"Struct"
Instance Method
- == : It is known as Equality. It returns true if str is equal to other_struct in terms of the values of instance variables. And also they must be of same class as created by Struct.new. Otherwise, it return false.
str == other_struct
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
other_struct = Geek. new ( "Java" , "array" )
str1 = Geek. new ( "Ruby" , "Struct" )
p str == other_struct
p str == str1
|
Output:
false
true
- [] : It is known as Attribute Reference. It returns the value of the instance variable named by symbol or index(0..length-1) by int. If the named variable does not exist, then it raises NameError and if the index is out of range then it raises IndexError.
str[symbol]
str[int]
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
p str[ :tutorial_name ]
p str[ "topic_name" ]
|
Output:
"Ruby"
"Struct"
- []= : It is known as Attribute Assignment. It is used to assign the instance variable name with a symbol or the value of obj by int and return it. If the name of the instance variable does not exist or if the index is out of range, then it raise NameError.
str[symbol] = obj
str[int] = obj
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
str[ :tutorial_name ]= "Java"
str[ :topic_name ]= "array"
p str.tutorial_name
p str.topic_name
|
Output:
"Java"
"array"
- each : This method call block for each instance variable and pass the value as a parameter.
str.each_pair{|obj| block}
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
str. each {|a| puts (a)}
|
Output:
Ruby
Struct
- each_pair : This method calls block for each instance variable and pass the name and value as parameter.
str.each_pair{|symbol, obj| block}
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
str.each_pair{|tutorial_name, a| puts ( "#{tutorial_name} => #{a}" )}
|
Output:
tutorial_name => Ruby
topic_name => Struct
- length : This method returns the number of instance variables. The return type of this method is an integer.
str.length
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
p str.length
|
Output:
2
- members : This method returns an array of string that represent the name of the instance variable.
str.members
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
p str.members
|
Output:
[:tutorial_name, :topic_name]
- size : This method is similar to Struct#length method. The return type of this method is an integer.
str.size
- to_a : This method returns the values for this instance as an array.
str.to_a
Example:
Geek = Struct. new ( :tutorial_name , :topic_name )
str = Geek. new ( "Ruby" , "Struct" )
p str.to_a[ 0 ]
p str.to_a[ 1 ]
|
Output:
"Ruby"
"Struct"
- values : This method is similar to Struct#to_a method.
str.values
- values_at : This method return an array that consist the element in str corresponding to the given indices. The selectors may be integer indices or range.
str.values_at([selector])
Example:
Geek = Struct. new ( :p , :q , :r , :s )
str = Geek. new ( 12 , 13 , 14 , 15 )
p str.values_at( 2 , 1 )
p str.values_at( 2 , 1 , 0 , 3 )
|
Output:
Geek
[14, 13]
[14, 13, 12, 15]
Reference: https://ruby-doc.org/core-2.2.0/Struct.html
Share your thoughts in the comments
Please Login to comment...