
Struct
A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class.
The Struct class generates new subclasses that hold a set of members and their values. For each member a reader and writer method is created similar to Module#attr_accessor.
So when we create a Struct in ruby, it creates attr_accessor methods for the symbols we provide to it, by doing so it saves us a lot of time, in order to get a deeper understanding of structs lets take an example of a struct, and a similar Class
## That's how you define a struct
Customer = Struct.new(:customer_id, :name, :address)
## That's How you use a struct
_bilal = Customer.new(1, "Bilal Haider", "Islamabad, Pakistan")
_bago = Customer.new(2, "Bagosan Sensei", "Belgium, Belgium")
puts _bilal[:customer_id]
puts _bilal[:name]
puts _bilal[:address]
puts _bago[:customer_id]
puts _bago[:name]
puts _bago[:address]

If we were to do the same thing, by writing a Class
It would take more lines of code check this same thing done in a Class style.
That is our classic method of programming,
- Identify entities
- Create a Class from them
- Instantiate objects from the Class
- Use them for your benefit

class Customer
attr_accessor :customer_id, :name, :address
def initialize(*options)
@customer_id = options[0]
@name = options[1]
@address = options[2]
end
end
_bilal = Customer.new(1, "Bilal Haider", "Islamabad, Pakistan")
_bago = Customer.new(2, "Bagosan Sensei", "Belgium, Belgium")
puts _bilal.customer_id
puts _bilal.name
puts _bilal.address
puts _bago.customer_id
puts _bago.name
puts _bago.address
So when we create a Struct it creates attr_accessor methods for each of the symbols passed to it, and in background it does all the work for us..
Structs are simple, and they save our time, from writing Classes with initialize methods/attr_accessor methods.
We can do the same thing, with just one line of code. :) if we do it in a Class style, we have to write quite a lot of lines of code.
If you are thinking, wait Bilal, we can add our own methods in Classes .. and it looks like we can't do that with Structs ??
So the answer to this question is, that you can also add your own methods to structs :)
Structs also allows us to define methods and use them accordingly

Customer = Struct.new(:customer_id, :name, :address) do
def customer_is_great
puts "#{name} is Great"
end
end
_bilal = Customer.new(1, "Bilal Haider", "Islamabad, Pakistan")
puts _bilal[:customer_id]
puts _bilal[:name]
puts _bilal[:address]
puts _bilal.customer_is_great
if we were to the same thing, in Class way, we need to add the method to our class, and Call it accordingly from our Object.
class Customer
attr_accessor :customer_id, :name, :address
def initialize(*options)
@customer_id = options[0]
@name = options[1]
@address = options[2]
end
def customer_is_great
puts "#{@name} is Great"
end
end
_bilal = Customer.new(1, "Bilal Haider", "Islamabad, Pakistan")
puts _bilal.customer_id
puts _bilal.name
puts _bilal.address
puts _bilal.customer_is_great

That's all about structs,
If you guys want to learn more about them, here is a reference link ..
https://ruby-doc.org/core-2.5.0/Struct.html
Finally here is the result from both struct.rb and customer.rb, If you are asking me which style of coding is better, I would say Writing a Class instead of Struct is better. but its not about which is better and which is not. but rather they both can be better in certain scenarios, e.g when we want to write an object which is not very complex, its better to write Struct
