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

in ruby •  7 years ago  (edited)

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

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]

Screenshot from 2018-03-26 14-04-36.png

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

Screenshot from 2018-03-26 14-10-57.png

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

Screenshot from 2018-03-26 14-26-44.png

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

Screenshot from 2018-03-26 14-29-25.png

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

Screenshot from 2018-03-26 14-33-07.png

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  7 years ago (edited)

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.

So cute

@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
Keep up the good work

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

If you don't want to join.. That's alright.

But I know the channel has got a lot for you to learn.

Nice meeting you too

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?