Friday 11 April 2014

Usage of Set in Ruby

In this article, I am going to explain when should we use a Set in ruby language. Lets go with some real examples. If you are planning to create a person address with few columns like name and address_detail. Simply you can use like this

PersonAddress = Struct.new(:name,:address_detail)

May be in some cases you have to create a list of  person addresses and want to list of those. That time you have use array like this.

address1 = PersonAddress.new("Madhan","12,taulk office road, Mannargudi")
address2 = PersonAddress.new("Ayyasamy","53/1,3rd cross,3rd main,4th cross, Laxmi Layout, Near subash cable office, Bangalore-560068")

If you want to make a list of person address you created then you have to push it into array like this

all_addresses = []
all_addresses << address1
all_addresses << address2 

Now you are thinking all_addresses should have uniqueness facility. This time what you will do for uniqueness of address. Either you have to use include? command or uniq! method. So code should be like this

all_addresses << address1 unless all_addresses.include?(address1)

or
all_addresses.uniq!

If you missed above way then duplicate will add into array.

all_addresses << address1
# => [address1]

all_addresses << address2
# => [address1,address2] all_addresses << address1
# => [address1,address1,address2] 

Now the time to think about "Set" class in ruby language. If you  want a result as a collection with unique then go for "Set". Lets look into following example.

require 'set'
all_addresses = Set.new

There is no modification on PersonAddress struct. Change is only on array variable only, instead of array we are using "Set" to store the address information.

So full code become as follows.
require 'set'
all_addresses = Set.new
PersonAddress = Struct.new(:name,:address_detail)
address1 = PersonAddress.new("Madhan","12,taulk office road, Mannargudi")
address 2 = PersonAddress.new("Ayyasamy","53/1,3rd cross,3rd main,4th cross, Laxmi Layout, Near subash cable office, Bangalore-560068")

all_addresses << address1
=> #}>
all_addresses << address2
=> #, #}>

Now if you are adding again duplicate entry like 
all_addresses << address1
# =>  #, #}>
2.0.0-p195 :041 >





See above result there is no chance when you add duplicate entry. Thats the real usage of "Set" in Ruby Language.

Conclusion of this post is, array also fine way to achieve the above scenario, if you want to accept duplicate. Better you need to define your data in proper way using proper classes available in ruby, that way you have a more control on your data. The data also will work as much you expect. Finally you should consider about performance also the classes provide flexibility. If you are not bothering about your way of implementation then you will end up with ugly code then later you have to revisit your code for better tuning in form of performance. So better you decide proper classes which you want to boost performance and control about your data as you expect.
Hopefully you can enjoy this article, will catch you later.

No comments :

Post a Comment