Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Collections in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 221
    Comment on it

    Whenever we code in any language, the first question arises that, what are the important Data collection in the corresponding Language contains.

    As we have Ruby language with us. We have some nice Data Collections here.

    • Array
    • Hash
    • Set

    Array:

    Arrays are the one of the pillar in Ruby. The frequently used collection consists of two square brackets. And Inside those square brackets, there is a series of data with corresponding Index.

    They can be heterogenous and even Arrays inside a Array.

    • Lets start with Creating Array
    array = []   //an Empty array
     => []
    
    arr = [1,2,3.4,6]  // Number Array
     => [1, 2, 3.4, 6]
    
    arr = [2,'good',54,32,'new'] //Heterogenous array
     => [2, "good", 54, 32, "new"]
    
    new = Array.new([1,2,3])  //Array constructed as an object
     => [1, 2, 3]
    •  We can create Dynamic Arrays by providing parameters.
    new = Array.new(4)
     => [nil, nil, nil, nil]

    Now, How about inserting some elements in array

    • For inserting we have << and it will push element at the last index.
    • while to remove an element from an array we have a method called shift. shift will remove element from the begining of index.
    new << 48   // It has inserted element from last of the Array
     => [nil, nil, nil, nil, 48]
    
    new.shift    // It has removed the 1st element of the Array
      => nil
    
    new   // This is how our array looks like after applying shift
     => [nil, nil, nil, 48]

    We have many methods in Array. Have a look link

    Hash:

    As Array is a Important pillar of collection in Ruby, Hashes are the another pillar.

    In Array all the values are assigned to a single variable. But in hashes you can have different values for different keys which are wrapped in curly braces.

    • To start with Hash, lets create some,
    mountains = {}  //empty hash
    • Now to create key Value pair in our hash we have to require a key
    > mountains[:peaks] = "Mt. Everest"
     => "Mt. Everest"
    
    if we print mountains we get
    
    > mountains
    => {:peaks=>"Mt. Everest"}
    • As Array can be used as objects, Similarly it works with Hashes also
    mountains = Hash.new
    
    mountains[:peaks]  // It will return nil
    => nil
    • Now, we will use some Important methods for Hash
    > mountains = {"Everest" => "8,848", "K2" =>"8,611 ","Kangchenjunga" =>"8,586" ,"Lhotse" =>"8,516","Makalu" =>"8,462" }
     => {"Everest"=>"8,848", "K2"=>"8,611 ", "Kangchenjunga"=>"8,586", "Lhotse"=>"8,516", "Makalu"=>"8,462"}
    
    > mountains.each do |key,value|
    >     puts "Mount. #{key}: #{value}"
    > end
    
    Mount. Everest: 8,848
    Mount. K2: 8,611
    Mount. Kangchenjunga: 8,586
    Mount. Lhotse: 8,516
    Mount. Makalu: 8,462

    Set:

    The collection which contains only unique values.

    To use Sets the first thing you need to do is to require it

    require 'set'

    The first thing to do is Create a Set

    • as object
    a = Set.new([1,2,3])
    => #<Set: {1, 2, 3}>
    • Creating an empty set and use some methods
    a = Set.new
     => #<Set: {}>
    
     a << 21
      => #<Set: {21}>
    
     a.add 87
     => #<Set: {21, 87}>
    
     a.add 44
     => #<Set: {21, 87, 44}>
    • Now, lets delete some elements
    a.delete 44
    => #<Set: {21, 87}>
    • Check if it contains any element

     

    a.include? 21
    => false
    >> a.include? 67
     true

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: