Ruby Part 3

Ruby Arrays & Hashes

Array - Initialization

array = []
array1 = [nil]
array2 = [10, 10]

Array - Index, Part 1

def element_at(arr, index)
    arr[index]
end

def inclusive_range(arr, start_pos, end_pos)
    arr[start_pos..end_pos]
end

def non_inclusive_range(arr, start_pos, end_pos)
    arr[start_pos...end_pos]
end

def start_and_length(arr, start_pos, length)
    arr[start_pos, length]
end

Array - Index, Part 2

def neg_pos(arr, index)
    arr[-index]
end

def first_element(arr)
    arr.first
end

def last_element(arr)
    arr.last
end

def first_n(arr, n)
    arr.first(n)
end

def drop_n(arr, n)
    arr.last(n)
end

Array - Addition

def end_arr_add(arr, element)
    arr.push(element)
end

def begin_arr_add(arr, element)
    arr.insert(0, element)
end

def index_arr_add(arr, index, element)
    arr.insert(index, element)
end

def index_arr_multiple_add(arr, index)
    arr.unshift(5, 15)
end

Array - Deletion

def end_arr_delete(arr)
    arr.pop
end

def start_arr_delete(arr)
    arr.shift
end

def delete_at_arr(arr, index)
    arr.delete_at(index)
end

def delete_all(arr, val)
    arr.delete(val)
end

Array - Selection

def select_arr(arr)
    arr.select {|a| a % 2 === 1}
end

def reject_arr(arr)
    arr.reject {|a| a % 3 === 0}
end

def delete_arr(arr)
    arr.delete_if {|a| a < 0}
end

def keep_arr(arr)
    arr.keep_if {|a| a >= 0}
end

Hash - Initialization

empty_hash = Hash.new
default_hash = Hash.new(1)
hackerrank = {"simmy" => 100, "vivmbbs" => 200}

Hash - Each

def iter_hash(hash)
    hash.each do |key, value|
        puts key
        puts value
    end
end

Hash - Addition, Deletion, Selection

hackerrank.store(543121,100)
hackerrank.keep_if { |key,value| key.is_a?(Integer) }
hackerrank.delete_if { |key,value| key % 2 == 0 }

Jenny Plunkett

Applications Engineer @ Arm