Ruby | Struct Class
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:
# Ruby program to illustrate # use of Struct # creating Struct# Geek is generated classGeek = Struct.new(:tut_name, :cate_name) do def gfg "This is #{cate_name} class tutorial in #{tut_name}." endend # creating object of structa = 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:
# Ruby program to illustrate # creating structure # Creating a structure with a name in structStruct.new("Geek", :tutorial_name, :topic_name) Struct::Geek.new("ruby", "Struct") # Create a structure named by its constantGeek = 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:
# Ruby program to illustrate # creating objects of structure # Create structureGeek = Struct.new(:tutorial_name, :topic_name) # Creating objectsstr = 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:
# Ruby program to illustrate# check equality# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")other_struct = Geek.new("Java","array")str1 = Geek.new("Ruby","Struct")# Check equalityp str == other_structp str == str1Output:
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:
# Ruby program to illustrate# use of []# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")# Using []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:
# Ruby program to illustrate# use of []=# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")# Using []=str[:tutorial_name]="Java"str[:topic_name]="array"p str.tutorial_namep str.topic_nameOutput:
"Java" "array"
- each : This method call block for each instance variable and pass the value as a parameter.
str.each_pair{|obj| block}Example:
# Ruby program to illustrate# use of each method# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")# Using each methodstr.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:
# Ruby program to illustrate# use of each_pair method# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")# Using each_pair methodstr.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:
# Ruby program to illustrate# use of length method# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")# Using the length methodp str.lengthOutput:
2
- members : This method returns an array of string that represent the name of the instance variable.
str.members
Example:
# Ruby program to illustrate# use of members# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")# Using members methodp str.membersOutput:
[: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:
# Ruby program to illustrate# use of to_a method# Create structureGeek = Struct.new(:tutorial_name,:topic_name)# Creating objectsstr = Geek.new("Ruby","Struct")# Using to_a methodp 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:
# Ruby program to illustrate# use of value_at method# Create structureGeek = Struct.new(:p,:q,:r,:s)# Creating objectsstr = Geek.new(12,13,14,15)# Using values_at methodp 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


Please Login to comment...