Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Discussion on hashes and symbols in Ruby

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 518
    Comment on it

    Welcome to Findnerd. Today we are going to discuss hashes and symbols in Ruby. We have written other blogs as well on Ruby so you can check these blogs for clear understanding of Ruby. If you have worked in other programming language then you should have a clear idea of arrays. We have discussed the indexed and ordered arrays in previous blog. Now we are going to discuss other form of arrays that is hashes. In other language like php, java we call it associate arrays but here we pronounce it hashes. Indexed array has integer numbers as a key but in hashes we use object or string as key. Please have a look.

     

    employee = ['Kunal','Sharma','Manager']
    # this ordered array with integer keys
    
    employee = {'first_name'=>'Kunal','last_name'=>'Sharma','position'=>'Manager'}
    // hash named employee

     

     

    In above code you can see, we created array employee. In ruby array is created with square brackets and hash is created with curly brackets. You can access the hash elements with its keys.

     

    employee['first_name']
    # Kunal
    employee['position']
    # Manager

     

     

    So you can see above code, we are using object index instead of integer index. You can also get the key by passing the value in function index.  Please have a look.

     

    employee.index('Kunal')
    // first_name

     

     

    You can also create a mixture of arrays and hashes. It means we can create hash with integer keys as well as object keys. Please have a look.

     

    mix_one = {"intro"=>"Welcome",[10,3] => 'Blogging', 2 => 'Hope'}
    
    mix_one[1]
    
    mix_one['intro']

     

     

    You can get all keys using keys function and all values using function values. Please have a look.

     

    mix.keys
    
    # [[10,3],1,'intro']
    
    mix_one.values
    # ["Blogging,"Hope",Welcome"]

     

     

    length and size functions will return the number of elements in hash.

     

    mix_one.length
    # 3
    mix_one.size
    # 3

     

     

    You can get the array in form of key and value pairs. Please have a look.

     

    mix_one.to_a
    
    # [ [ [10,3] ,"Blogging"] , [1,"Hope"] ,["intro","Welcome"] ] ]

     

     

    clear will empty the hash. Please have a look.

     

    mix_one.clear
    # {}

     

     

    We have discussed the string and variable in our previous blogs. Now we will discuss the symbols. Symbol is not a string or variable. It is used as label that is used to identify the data. You don't need to confuse with symbol. We will explain its working below.

     

    If you make difference in string and symbol then you will see that string stores in memory every time when it created but symbol stores in memory one time. Please have a look.

     

    :master
    # return :master
    
    "master_str".object_id
    # return string for exp: 43433
    
    :master.object_id
    # return the symbol id which will be same for the symbol everytime you fetch

     

     

    You can see in above code whenever we get the string id, it will return different ids on every turn but It will return the same object id for the symbol.

     

    If you try to assign any value to symbol then it will return an error that you can not assign the value to symbol.

     

    :master = 2
    // error occurred

     

     

    You can use symbols in hashes as keys. Please have a look.

     

    $sym_hash = {:first_name=>"Deepak", :last_name => "Verma"}
    
    $syn_hash[:first_name]
    
    # Deepak

     

     

    You can say, symbols are used to transfer piece of data on different pages in application.

     


    Thank you for being with us!

 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: