Ruby | Array keep_if() function Last Updated : 06 Dec, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Array#keep_if() : keep_if() is a Array class method which returns the array element that satisfy the given condition. Syntax: Array.keep_if() Parameter: Array Return: the array element that satisfy the given condition. Example #1 : Ruby # Ruby code for keep_if() method # declaring array a = [18, 22, 33, 3, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 3, 3, 50, 6] # keep_if puts "keep_if method : #{a.keep_if {|num| num > 10 }}\n\n" # keep_if puts "keep_if method : #{b.keep_if {|x| x.odd? }}\n\n" Output : keep_if method : [18, 22, 33] keep_if method : [1, 1, 1, 9] Example #2 : Ruby # Ruby code for keep_if() method # declaring array a = [18, 22, 33, 3, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 3, 3, 53, 6] # keep_if puts "keep_if method : #{c.keep_if {|num| num > 10 }}\n\n" # keep_if puts "keep_if method : #{c.keep_if {|num| num.even? }}\n\n" Output : keep_if method : [18, 22, 53] keep_if method : [18, 22] Comment More infoAdvertise with us Next Article Ruby | Array pop() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Array-class Similar Reads Ruby | Array one?() function Array#one?() : one?() is a Array class method which checks whether the array is having only one array element. Syntax: Array.one?() Parameter: Array Return: true - if array has only one element otherwise return false. Example #1 :Â Ruby # Ruby code for one?() method # declaring array a = [18, 22, 33 1 min read Ruby | Array pop() function Array#pop() : pop() is a Array class method which checks removes the last element from the array and returns it. Syntax: Array.pop() Parameter: Array Return: removes the last element from the array and returns it. Example #1 : Ruby # Ruby code for pop() method # declaring array a = [18, 22, 33, nil, 1 min read Ruby | Array map() function Array#map() : map() is a Array class method which returns a new array containing the values returned by the block. Syntax: Array.map() Parameter: Array Return: a new array containing the values returned by the block. Example #1 : Ruby # Ruby code for map() method # declaring array a = [18, 22, 33, 3 2 min read Ruby | Array min() function Array#min() : min() is a Array class method which returns the minimum value in this array. Syntax: Array.min() Parameter: Array Return: the minimum value in this array. Example #1 : Ruby # Ruby code for min() method # declaring array a = %w[for geeks] # min method example puts "min() method for 1 min read Ruby | Array rindex() function Array#rindex() : rindex() is a Array class method which returns the index of the last object in the array. Syntax: Array.rindex() Parameter: Array Return: the index of the last object in the array. if not present then nil Example #1 : Ruby # Ruby code for rindex() method # declaring array a = [18, 2 1 min read Ruby | Array reject!() function Array#reject!() : reject!() is a Array class method which returns a new array containing the values which are not returned by the block. Syntax: Array.reject!() Parameter: Array Return: a new array containing the values not returned by the block. Example #1 : Ruby # Ruby code for reject!() method # 1 min read Like