Ruby | Array take_while() function Last Updated : 06 Dec, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report Array#take_while() : take_while() is a Array class method which returns elements to the block until the block returns nil or false. Syntax: Array.take_while() Parameter: Array Return: elements to the block until the block returns nil or false. Example #1 : Ruby # Ruby code for take_while() 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] # take_while puts "take_while method : #{a.take_while {|num| num > 10 }}\n\n" # take_while puts "take_while method : #{b.take_while {|x| x.odd? }}\n\n" Output : take_while method : [18, 22, 33] take_while method : [1] Example #2 : Ruby # Ruby code for take_while() 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] # take_while puts "take_while method : #{c.take_while {|num| num > 10 }}\n\n" # take_while puts "take_while method : #{c.take_while {|num| num.even? }}\n\n" Output : take_while method : [18, 22] take_while method : [18, 22] Comment More infoAdvertise with us Next Article Ruby | Array take_while() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Array-class Similar Reads Ruby | Array take() function Array#take() : take() is a Array class method which returns the number of elements in the array. Syntax: Array.take() Parameter: Array Return: the number of elements in the array. Example #1 : Ruby # Ruby code for take() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 1 min read Ruby | Enumerable take_while() function The take_while() of enumerable is an inbuilt method in Ruby returns all the elements till the first element which returns a false to the condition in the block. It returns an enumerator if the block is not passed. Syntax: enu.take_while {|obj| block|} Parameters: The function accepts a block. Return 1 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 select() function 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. Ex 2 min read 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 Like