Ruby | Array select() function Last Updated : 06 Dec, 2019 Comments Improve Suggest changes Like Article Like Report Array#select() : select() is a Array class method which returns a new array containing all elements of array for which the given block returns a true value. Syntax: Array.select() Parameter: Array Return: A new array containing all elements of array for which the given block returns a true value. Example #1 : Ruby # Ruby code for select() 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] # select puts "select method : #{a.select {|num| num > 10 }}\n\n" # select puts "select method : #{b.select {|x| x.odd? }}\n\n" Output : select method : [18, 22, 33] select method : [1, 1, 1, 9] Example #2 : Ruby # Ruby code for select() 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] # select puts "select method : #{c.select {|num| num > 10 }}\n\n" # select puts "select method : #{c.select {|num| num.even? }}\n\n" Output : select method : [18, 22, 53] select method : [18, 22, 6] Comment More infoAdvertise with us Next Article Ruby | Array select() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Array-class Similar Reads Ruby | Array select!() function Array#select!() : select!() is a Array class method which returns the given block passing in successive elements from self, deleting elements for which the block returns a false value. Syntax: Array.select!() Parameter: Array Return: the given block passing in successive elements from self, deleting 2 min read Ruby | Array slice() function Array#slice() : slice() is a Array class method which returns a subarray specified by range of indices. Syntax: Array.slice() Parameter: Array Return: a subarray specified by range of indices. Example #1 : Ruby # Ruby code for slice() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring 1 min read Ruby | Array slice!() function Array#slice!() : slice!() is a Array class method which seletes the element(s) given by an index or by a range. Syntax: Array.slice!() Parameter: Array Return: Deletes the elements given by an index or by a range. Example #1 : Ruby # Ruby code for slice!() method # declaring array a = [18, 22, 33, n 2 min read Ruby | Array sample() function Array#sample() : sample() is a Array class method which returns a random element or n random elements from the array. Syntax: Array.sample() Parameter: Array Return: a random element or n random elements from the array. Example #1 : Ruby # Ruby code for sample() method # declaring array a = [18, 22, 1 min read Ruby | Array to_s() function Array#to_s() : to_s() is a Array class method which returns self array. Syntax: Array.to_s() Parameter: Array Return: self array Example #1 : Ruby # Ruby code for to_s() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50, 1 min read Like