Ruby | Range first() function Last Updated : 19 Mar, 2024 Comments Improve Suggest changes Like Article Like Report The first() is an inbuilt method in Ruby returns an array of first X elements. If X is not mentioned, it returns the first element only. Syntax: range1.first(X) Parameters: The function accepts X which is the number of elements from the beginning. Return Value: It returns an array of first X elements. Example 1: Ruby # Ruby program for first() # method in Range # Initialize range range1 = (0..10) # Prints the first element puts range1.first() Output: 0 Example 2: Ruby # Ruby program for first() # method in Range # Initialize range range1 = (0..10) # Prints the first element puts range1.first(3) Output: 0 1 2 Comment More infoAdvertise with us Next Article Ruby | Range first() function gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Range-class Similar Reads Ruby | Range entries() function The entries() is an inbuilt method in Ruby returns an array containing all elements of the given range. Syntax: range1.entries() Parameters: The function accepts no parameter. Return Value: It returns an array containing all elements of the given range. Example 1: Ruby # Ruby program for entries() # 1 min read Ruby | Range begin() function The begin() is an inbuilt method in Ruby returns the first element of the given range. Syntax: range1.begin() Parameters: The function accepts no parameter Return Value: It returns the first element of the given range. Example 1: Ruby # Ruby program for begin() method in Range # Initialize range ran 1 min read Ruby | Range cover?() function The cover?() is an inbuilt method in Ruby returns a boolean value true if the given object lies within the given range, else it returns false. The object can be an element or a range. Syntax: range1.cover?(obj) Parameters: The function accepts an object which is to be checked for. Return Value: It r 1 min read Ruby | Range end() function The end() is an inbuilt method in Ruby returns the last element of the given range. Syntax: range1.end() Parameters: The function accepts no parameter Return Value: It returns the last element of the given range. Example 1: Ruby # Ruby program for end() method in Range # Initialize range range1 = (0 1 min read Ruby | Range eql?() function The eql?() is an inbuilt method in Ruby returns boolean value true if both the given ranges are equal, else it returns false. Syntax: range1.eql?(range2) Parameters: The function accepts no parameter Return Value: It returns boolean value true if both the given ranges are equal, else it returns fals 1 min read Ruby | Range each() function The each() is an inbuilt method in Ruby iterates over every element in the range. Syntax: range1.each(|el| block) Parameters: The function accepts a block which specifies the way in which the elements are iterated. Return Value: It returns every elements in the range. Example 1: Ruby # Ruby program 1 min read Ruby | Range to_a() function The to_a() is an inbuilt method in Ruby returns an array containing the numbers in the given range. Syntax: range1.to_a() Parameters: The function accepts no parameter. Return Value: It returns an array containing all the numbers. Example 1: Ruby # Ruby program for to_a() # method in Range # Initial 1 min read Ruby | Range new() function The new() is an inbuilt method in Ruby returns a new range of numbers. Syntax: range1.new(first, last) Parameters: The function accepts first and last which is the range that is to be created. Return Value: It returns the range of numbers. Example 1: Ruby # Ruby program for new() # method in Range # 1 min read Ruby | Range last() function The last() is an inbuilt method in Ruby returns an array of last X elements. If X is not mentioned, it returns the last element only. Syntax: range1.last(X) Parameters: The function accepts X which is the number of elements from the end. Return Value: It returns an array of last X elements. Example 1 min read Ruby | Range hash() function The hash() is an inbuilt method in Ruby returns a hash-code for the given range. The hash-value will vary for every execution. Syntax: range1.hash() Parameters: The function accepts no parameter. Return Value: It returns a hash-code for the given range. Example 1: Ruby # Ruby program for hash() # me 1 min read Like