1> To delete an element from an array, use reject as shown below:
arr = ['a','b','c','d','e']
arr.reject! { |i| i.match('d')}
Result :=> ["a", "b", "c", "e"]
2> To delete particular keys from the array whose elements are also arrays, use reject as follows:
Ex: If we have to delete the even keys from the given array, then use reject in a block as
a = [ [ 1, "one"], [2, "two"], [3, "three"], [4, "four"] ]
a.reject!{ |key,value| key%2==0}
Result :=> [[1, "one"], [3, "three"]]
0 Comment(s)