Ruby Programming Tutorial - Lesson 37 - Structs or Structures in Ruby

in #ruby7 years ago (edited)

201605311142510400_Want-twins-So-dry-this_SECVPF.gif.jpeg

<h2>Struct <blockquote> <p dir="auto">A Struct is a convenient way to bundle a number of attributes together, using accessor methods, without having to write an explicit class. <p dir="auto">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. <p dir="auto">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 <pre><code>## 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] <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmbVeaqcKZbZ3bwxt9qx3KHtL5jH4WE3wFyJGSVnfQLx59/Screenshot%20from%202018-03-26%2014-04-36.png" alt="Screenshot from 2018-03-26 14-04-36.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmbVeaqcKZbZ3bwxt9qx3KHtL5jH4WE3wFyJGSVnfQLx59/Screenshot%20from%202018-03-26%2014-04-36.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmbVeaqcKZbZ3bwxt9qx3KHtL5jH4WE3wFyJGSVnfQLx59/Screenshot%20from%202018-03-26%2014-04-36.png 2x" /> <h2>If we were to do the same thing, by writing a Class <p dir="auto">It would take more lines of code check this same thing done in a Class style. <p dir="auto">That is our classic method of programming, <ul> <li>Identify entities <li>Create a Class from them <li>Instantiate objects from the Class <li>Use them for your benefit <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmRsWwF5xaBmVf34ULwVSckPMqKxHvVNnjCzVsM39A41Xd/Screenshot%20from%202018-03-26%2014-10-57.png" alt="Screenshot from 2018-03-26 14-10-57.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmRsWwF5xaBmVf34ULwVSckPMqKxHvVNnjCzVsM39A41Xd/Screenshot%20from%202018-03-26%2014-10-57.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmRsWwF5xaBmVf34ULwVSckPMqKxHvVNnjCzVsM39A41Xd/Screenshot%20from%202018-03-26%2014-10-57.png 2x" /> <pre><code>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 <p dir="auto">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.. <p dir="auto">Structs are simple, and they save our time, from writing Classes with initialize methods/attr_accessor methods.<br /> 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. <p dir="auto">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 ?? <p dir="auto">So the answer to this question is, that you can also add your own methods to structs :) <blockquote> <p dir="auto">Structs also allows us to define methods and use them accordingly <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmNqYLTZckJjUNsxd8nYUX9sZa8sdcV88d5uf1ssKdJsPc/Screenshot%20from%202018-03-26%2014-26-44.png" alt="Screenshot from 2018-03-26 14-26-44.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmNqYLTZckJjUNsxd8nYUX9sZa8sdcV88d5uf1ssKdJsPc/Screenshot%20from%202018-03-26%2014-26-44.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmNqYLTZckJjUNsxd8nYUX9sZa8sdcV88d5uf1ssKdJsPc/Screenshot%20from%202018-03-26%2014-26-44.png 2x" /> <pre><code>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 <p dir="auto">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. <pre><code>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 <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmQgsGfrJTp8zn4yJycFAE1HsFeZ9BKrWX7jKcmfEWCUNR/Screenshot%20from%202018-03-26%2014-29-25.png" alt="Screenshot from 2018-03-26 14-29-25.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmQgsGfrJTp8zn4yJycFAE1HsFeZ9BKrWX7jKcmfEWCUNR/Screenshot%20from%202018-03-26%2014-29-25.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmQgsGfrJTp8zn4yJycFAE1HsFeZ9BKrWX7jKcmfEWCUNR/Screenshot%20from%202018-03-26%2014-29-25.png 2x" /> <p dir="auto">That's all about structs,<br /> If you guys want to learn more about them, here is a reference link ..<br /><span> <a href="https://ruby-doc.org/core-2.5.0/Struct.html" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">https://ruby-doc.org/core-2.5.0/Struct.html <p dir="auto">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 <p dir="auto"><img src="https://images.hive.blog/768x0/https://steemitimages.com/DQmYgrexXfEeBrtL23FzR14tLU3rVpaZNw21DCZzqRuYJj8/Screenshot%20from%202018-03-26%2014-33-07.png" alt="Screenshot from 2018-03-26 14-33-07.png" srcset="https://images.hive.blog/768x0/https://steemitimages.com/DQmYgrexXfEeBrtL23FzR14tLU3rVpaZNw21DCZzqRuYJj8/Screenshot%20from%202018-03-26%2014-33-07.png 1x, https://images.hive.blog/1536x0/https://steemitimages.com/DQmYgrexXfEeBrtL23FzR14tLU3rVpaZNw21DCZzqRuYJj8/Screenshot%20from%202018-03-26%2014-33-07.png 2x" />
Sort:  

Great contents. Thanks for following! :D Followed you back.

You are welcome :) are you a programmer?

@bilal-haider if you can create a program to update the mobile app, from android nougat to android oreo.

I cannot, have no idea about android nougat and android oreo .. I left mobile apps and games long ago.


Keep up the good work@bilal-haider Although I don't understand Ruby language, I could appreciate what you try to do. I am a mobile app developer and I appreciate your sharing of this knowledge

Thank you for appreciation, I also used to write android applications, you are using eclipse or android studio? or unity

I am not a programmer and I attempted to learn Andriod Studio but finally ended up using template driven mobile app builder.
I also use Game Maker Studio by yoyogames for my arcade games.
cheers

Wow... Such talent.. I use android. Would love to see some of the application you have written.
.if you aren't on the Steemschool discord channel, then please do join us using this link
https://discord.gg/2F74XX9

You can also teach us.. Look forward to seeing you there.

copil copil drăguț .. copil

Hello dear.. Feel free to join the Steemschool discord channel using this link
https://discord.gg/2F74XX9

Look forward to seeing you there.. A place for interaction and learning to help your growth on steemit.

Thanks.

You welcome... When you are there.. Holla @biljed will be sure to welcome you.

You will have an amazing and educative stay.

How can.

https://discord.gg/2F74XX9

Join us on the discord channel with the link

dispute. what we disagree if we want to join

What a beautiful baby Look very beautiful I like this baby very much. Really, wonderful baby

Hello dear, thanks for following us.You boosted our morals by following us.How are you today?