Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Arrays in Ruby and its uses

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 246
    Comment on it

    Welcome to Findnerd. Today we are going to discuss arrays and its uses in Ruby. We use  arrays in Ruby in the same way as we use in other programming languages. We all knows array is nothing but an ordered and integer based indexed collection of objects. We can simply create an array and assign the values to it. Please have a look.

     

    ruby_arr = ["Welcome","TO", "Findnerd"]

     

     

    You can access the elements using its index value. In above array we have three elements with three indexed values, starting from zero. Please have a look.

     

    ruby_arr[2]
    # return "TO"
    
    ruby_arr[5]
    # return nil because we have three elements in array 

     

     

    Ruby does not return error if indexed value is not exist. You can also assign value to particular index. Please have a look.

     

    ruby_arr[2] = "Great"

     

     

    You can add element in array using below code. Please have a look.

     

    ruby_arr << "Web"

     

    You can empty the array using clear method. Please have a look.

     

    ruby_arr.clear
    

     

     

    You can get the class using class method.

     

    ruby_arr.class
    
    # return Array

     

     

    An array can contain different type of elements like string, integer, floats and array as well. Please have a look.

     

    ruby_arr = ['Welcome',3,'Findnerd',4.0,[4,3]]

     

    There is a method inspect that will return the array elements as a string.

     

    ruby_arr.inspect

     

     

    If you use puts method then it will print the array element with new-line.

     

    puts ruby_arr

     

     

    There is method to_s that will return the elements as a string.

     

    ruby_arr.to_s
    # return "Welcome3Findnerd4.043"

     

    join method will combine the array elements with specific character. Please have a look.

     

    ruby_arr.join
    # return "Welcome3Findnerd4.043"
    
    ruby_arr.join(',')
    # return "Welcome,3,Findnerd,4.0,4,3"

     

     

    You can create array from a string by using common special character. Please have a look.

     

    str = "1,2,3,4,5,6";
    ruby_arr = str.split(',')
    # return ['1','2','3','4','5','6']

     

     

    You can reverse the array as well. Please have a look.

     

    ruby_arr.reverse

     

     

    You can sort the array using sort function. Please have a look.

     

    a = [3,2,5,4,1]
    a.sort
    # return [1,2,3,4,5]

     

     

    uniq method will delete the duplicate element.

     

     

    x = [3,4,5,4];
    x.uniq
    # return [3,4,5]

     

     

    You can even delete the element if you do not know the indexed value for the element. Please have a look.

     

    x = [2,4,5,6];
    x.delete(5)

     

     

    You can also add element using push method and remove the last element using pop method.

     

    x = [2,3,4,5,6,7];
    x.push(8);
    # return [2,3,4,5,6,7,8]
    
    x.pop
    # return 8
    # Now array [2,3,4,5,6,7]

     

     

    You can add two or more arrays using plus operator and remove the elements using minus operator. Please have a look.

     

    x = [1,2,3]
    ruby_arr = x + [4,5]
    
    # return [1,2,3,4,5]
    
    
    new_arr = ruby_arr - [1,4]
    # return [2,3,5]
    

     

     

    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: