Open In App

Ruby | Struct Class

Last Updated : 18 Sep, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

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


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 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")


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 structure
Geek = Struct.new(:tutorial_name, :topic_name)
  
# Creating objects
str = Geek.new("Ruby", "Struct")
p str.tutorial_name 
p str.topic_name


Output:

"Ruby"
"Struct"

Instance Method

  1. == : 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 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

    
    

    Output:

    false
    true
    
  2. [] : 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 structure
    Geek = Struct.new(:tutorial_name, :topic_name)
      
    # Creating objects
    str = Geek.new("Ruby", "Struct")
      
    # Using []
    p str[:tutorial_name]
    p str["topic_name"]

    
    

    Output:

    "Ruby"
    "Struct"
    
  3. []= : 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 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

    
    

    Output:

    "Java"
    "array"
    
  4. 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 structure
    Geek = Struct.new(:tutorial_name, :topic_name)
      
    # Creating objects
    str = Geek.new("Ruby", "Struct")
      
    # Using each method
    str.each{|a| puts (a)} 

    
    

    Output:

    Ruby
    Struct
    
  5. 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 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}")}

    
    

    Output:

    tutorial_name => Ruby
    topic_name => Struct
    
  6. 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 structure
    Geek = Struct.new(:tutorial_name, :topic_name)
      
    # Creating objects
    str = Geek.new("Ruby", "Struct")
      
    # Using the length method
    p str.length

    
    

    Output:

    2
    
  7. 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 structure
    Geek = Struct.new(:tutorial_name, :topic_name)
      
    # Creating objects
    str = Geek.new("Ruby", "Struct")
      
      
    # Using members method
    p str.members

    
    

    Output:

    [:tutorial_name, :topic_name]
    
  8. size : This method is similar to Struct#length method. The return type of this method is an integer.
    str.size
  9. 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 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]

    
    

    Output:

    "Ruby"
    "Struct"
    
  10. values : This method is similar to Struct#to_a method.
    str.values
  11. 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 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)

    
    

    Output:

    Geek
    [14, 13]
    [14, 13, 12, 15]
    

Reference: https://ruby-doc.org/core-2.2.0/Struct.html



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads