Home » Ruby programming

Ruby Set

Set in Ruby: In this tutorial, we are going to learn about the sets in Ruby programming language with examples.
Submitted by Hrithik Chandra Prasad, on October 11, 2019

Set in Ruby

In Ruby, the set is a class that is very similar to an array but it has some special attributes which make the processing faster. You must have observed that you can insert duplicate elements in an array but this is not the case of the set. The set does not allow the insertion of non-unique elements implicitly. So, in the future, if you will add elements after the definition of a set, you need not worry about the duplicity. It would not allow the accommodation of repeated elements. Following is the way to declare and define the Ruby set.

    set_name = Set.new([element1,element2,...])

Now, let us see how we declare, define and implement a set with the help of some supporting examples?

Example 1:

=begin
Ruby program to demonstrate set 
=end

require 'set'

# Creation of new Set.
Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

puts "Number of elements in set are #{Vegetable.size()}"
puts Vegetable

Output

Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>

In the above example, you can observe that we are creating a set named Vegetable as per the syntax given above. You can also observe that we need to put the statement require 'set' before starting to write code. We are only calculating the number of elements present in the set with the help of the set.size() method. Writing the set name along with the puts statement simply gives us the information about the set elements.

We have seen how to add elements in the set at the time of the declaration? Now, let us see how to add elements afterward?

Example 2:

=begin
Ruby program to demonstrate set 
=end

require 'set'

# Creation of new Set.
Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

puts "Number of elements in set are #{Vegetable.size()}"

puts Vegetable

Vegetable.add("Cauliflower")

Vegetable.add("Peas")

Vegetable.add("Beetroot")

puts "Number of elements in set are #{Vegetable.size()}"

puts Vegetable

Output

Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>
Number of elements in set are 7
#<Set: {"potato", "tomato", "brinjal", "onion", "Cauliflower", "Peas", "Beetroot"}>

In the code given above, you can observe that we are adding elements in the set Vegetable afterward with the help of set.add("Element") method. Then we are again printing the set size and the elements.

Example 3:

=begin
Ruby program to demonstrate set 
=end

require 'set'

# Creation of new Set.
Vegetable = Set.new(["potato", "tomato","brinjal","onion"])

puts "Number of elements in set are #{Vegetable.size()}"

puts Vegetable

Vegetable.add("potato")

Vegetable.add("tomato")

Vegetable.add("Beetroot")

puts "Number of elements in set are #{Vegetable.size()}"

puts Vegetable

Output

Number of elements in set are 4
#<Set: {"potato", "tomato", "brinjal", "onion"}>
Number of elements in set are 5
#<Set: {"potato", "tomato", "brinjal", "onion", "Beetroot"}>

In the above code, after observing the output you must be thinking that the size of the set is 5 even after adding 3 more elements. This is because the elements which we were trying to add are already present in the set. It doesn’t allow the repetition of elements. You will not get an error but the duplicate elements will be discarded.







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.