Ruby | Array take() function Last Updated : 06 Dec, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report 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, 4, 1, 1, 88, 9] # declaring array c = [18, 22, 50, 6] # take method example puts "take() method form : #{a.take(2)}\n\n" puts "take() method form : #{b.take(1)}\n\n" puts "take() method form : #{c.take(3)}\n\n" Output : take() method form : [18, 22] take() method form : [1] take() method form : [18, 22, 50] Example #2 : Ruby # Ruby code for take() method # declaring array a = ["abc", "nil", "dog"] # declaring array c = ["cat", nil] # declaring array b = ["cow", nil, "dog"] # take method example puts "take() method form : #{a.take(2)}\n\n" puts "take() method form : #{b.take(1)}\n\n" puts "take() method form : #{c.take(3)}\n\n" Output : take() method form : ["abc", "nil"] take() method form : ["cow"] take() method form : ["cat", nil] Comment More infoAdvertise with us Next Article Ruby | Array take() function M mayank5326 Follow Improve Article Tags : Ruby Ruby-Methods Ruby Array-class Similar Reads Ruby | Array take_while() function 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 # 1 min read Ruby | Array to_a() function Array#to_a() : to_a() is a Array class method which returns self array. Syntax: Array.to_a() Parameter: Array Return: self array representation Example #1 : Ruby # Ruby code for to_a() method # declaring array a = [18, 22, 33, nil, 5, 6] # declaring array b = [1, 4, 1, 1, 88, 9] # declaring array c 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 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 Ruby | Array to_h() function Array#to_h() : to_h() is a Array class method which returns the result of interpreting ary as an array of [key, value] pairs. Syntax: Array.to_h() Parameter: Array Return: the result of interpreting ary as an array of [key, value] pairs. Example #1 : Ruby # Ruby code for to_h() method # declaring ar 1 min read Like