Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Array and operations on array

    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 266
    Comment on it

    Array is used to keep collection of objects. Array in ruby can have any type of objects together. Using array in ruby:

    >> a = []
    >> arr = [1, 0, 7]
    >> arr[2] # => 7
    >> arr.size # => 3
    
    >> arr = ["string", 5, 0.8, true]

     

    You can create an object of Array like this:

    >> arr = Array.new([8,6,9]) #=> [8, 6, 9]

     

    However ruby developer community prefer the symbolic way to define new array:
     

    arr = []

    Note: Array in ruby is dynamic in size, so need not to pass the size of array while create a new array object.


     

    Pushing elements in array:

    arr = []
    
    arr.push(5) # => [5]
    OR
    arr << 5 #=> [5]
    
    arr.push(3,4,1) # => [5,3,4,1]

     

    Accessing value in array:

    arr[0] # => 5
    arr[-1] # => 1
    arr.first # => 5
    arr.last # => 1

     

    Removing elements of array:

    arr # => [5,3,4,1]
    
    remove from top:
    arr.pop # => 1
    
    remove from bottom:
    arr # => [5,3,4,1]
    
    arr.shift # => 5
    arr #=> [3,4,1]


     

    Array of string:

    %w{ this is an array}
    #=> ["this", "is", "an", "array"]


    Delete element in array:

    arr = [8,9,6,9,3]
    
    arr.delete(9)
    #=> 9
    arr
    #=> [8,6,3]   removed all 9 from array
    
    arr = [8,9,6,9,3]
    
    arr.delete_at(3)  # delete at a given index
    # => 9
    
    arr
    # => [8, 9, 6, 3]


    Join in array:

    arr = ["hello", "how", "are", "you"]
    arr.join(" ")
    # => "hello how are you"


    Flatten in array (flat nested arrays):

     arr = [1,[2,3],[4,["a", nil]]]
     arr.flatten
     # => [1,2,3,4,"a",nil]


    Compact in array (remove nil):

    arr = [1, 5, "a", nil, 9, nil, 6]
    arr.compact
    # => [1, 5, "a", 9, 6]


    inject with array:

    [1,2,3].inject {|sum, n| sum + n } # take first element in sum
    # => 6
    
    inject can also take an initial value
    
    [1, 2, 3].inject(5){|sum,x| sum + x }
    # => 11
    
    [1, 2, 3].inject(1){|product, x| product*x }
    # => 6
    
    [8, 9, 6, 9, 3].inject(0, :+) # in ruby 1.9 or above
    # => 35

     


    Iterators with array:

    [1,2,3].each { |i| i + 1 } # each returns the array acted upon
    => [1,2,3]
    
    [1,2,3].map { |i| i + 1 } # map returns new array
    # => [2, 3, 4]

     

    Shortcut methods with array:

    [1, 2, 3].map(&:to_s)
     => ["1", "2", "3"]
    
    ["a", "b", "c"].map(&:upcase)
    # => ["A", "B", "C"]


    More methods:

    ["a","b","c"].include? "d" # if array include the element
    # => false
    
    ["a","b","c"].include? "a"
    # => true
    
    ["a","b","c"].size
    # => 3


    Range to array:

    ('a'..'i').to_a
     => ["a", "b", "c", "d", "e", "f", "g", "h", "i"]
    
    (1..9).to_a
    #=>[1, 2, 3,  arr = [1,[2,3],[4,["a", nil]]]
     arr.flatten
     # => [1,2,3,4,"a",nil]4, 5, 6, 7, 8, 9]

     

 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: