Ruby | Array take() function Last Updated : 06 Dec, 2019 Comments Improve Suggest changes 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 Ruby | Array sort() function Array#sort() : sort() is a Array class method which returns a new array created by sorting self Syntax: Array.sort() Parameter: Array Return: a new array created by sorting self Example #1 : Ruby # Ruby code for sort() method # declaring array a = ["abc", "nil", "dog"] 1 min read Ruby | Array sort!() function Array#sort!() : sort!() is a Array class method which returns sorted self array in place. Syntax: Array.sort!() Parameter: Array Return: sorted self array in place. Example #1 : Ruby # Ruby code for sort!() method # declaring array a = ["abc", "nil", "dog"] # declaring 1 min read Ruby | Array transpose() function Array#transpose() : transpose() is a Array class method which returns the length of elements in the array transposes the rows and columns. Syntax: Array.transpose() Parameter: Array Return: transposes the rows and columns. Example #1 : Ruby # Ruby code for transpose() method # declaring array a = [[ 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 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 Like