Ruby | String <=> Method Last Updated : 07 Jan, 2020 Summarize Comments Improve Suggest changes Share Like Article Like Report <=>() is a String class method in Ruby which is used for comparisons of the strings. This method returns -1, 0, +1, or nil depending on whether the specified string is less than, equal to, or greater than other_string. Syntax: str other_string Parameters: Here, str and other_string are the strings which are compared. Returns: Returns nil if the two specified values are incomparable. Note: The longer string is considered greater than the shorter one if the specified strings are of different lengths, and the strings are equal when compared up to the shortest length, Example 1: Ruby #ruby 2.3.1 # Ruby program to demonstrate # the <=> method # Taking a string and # using the method puts "Ruby" <=> "Ruby" puts "Hello" <=> "Hell" Output: 0 1 Example 2: Ruby #ruby 2.3.1 # Ruby program to demonstrate # the <=> method # Taking a string and # using the method puts "ayucdef" <=> "ayucdefg" # return nil for this puts "Ruby" <=> 77 Output: -1 Comment More infoAdvertise with us Next Article Ruby | String == method K Kirti_Mangal Follow Improve Article Tags : Ruby Ruby-Methods Ruby String-class Similar Reads Ruby | String << Method <<() is a String class method in Ruby which is used to append the given object to the string. If the object is an integer, then by default it is considered a codepoint and converted to a character before being appended. Syntax: str << "object" Parameters: Here, str is the speci 1 min read Ruby | String getbyte Method getbyte is a String class method in Ruby which is used to return the indexth byte as an integer. Syntax: str.getbyte(index) Parameters: Here, str is the given string. Returns: Indexth byte as an integer. Example 1: Ruby # Ruby program to demonstrate # the getbyte method # Taking a string and # using 1 min read Ruby | String eql? Method eql? is a String class method in Ruby which is used to check whether the strings are equal or not if they have the same length and content. Syntax: str.eql?(Other_str) Parameters: Here, str and other_str are the strings. Returns: True or false basis on the equality. Example 1: Ruby # Ruby program to 1 min read Ruby | String * Method String# * is a String class method in Ruby which is used to returns a new String containing integer copies of the receiver. Here, integer must be greater than or equal to 0. Syntax: str * Integer Parameters: Here, str is the required string and integer is the number of copies. Returns: This method r 1 min read Ruby | String == method ==() is a String class method in Ruby which is used to check whether str==obj or not. Syntax: str == obj Parameters: Here, str is the given string and obj is the object to be compared. Returns: True or False based on the equality of the two strings. Example 1: Ruby #ruby 2.3.1 # Ruby program to demo 1 min read Ruby | String === Method ===() is a String class method in Ruby which is used to check whether str==obj or not. This method is similar to == method of string class. Syntax: str === obj Parameters: Here, str is the given string and obj is the object to be compared. Returns: True or False based on the equality of the two stri 1 min read Like