Today I was working mainly with Ruby strings. It still surprises me how many of these methods cross over into Javascript, although they have different names. Let's explore some of the essentials Ruby string methods and what they do:
.reverse
This method returns a new string with the characters in reverse order. For example:
.upcase
returns a new string with all characters converted to uppercase. Example:
.downcase
returns a new string with all characters converted to lowercase. Example:
.swapcase
returns a new string with uppercase characters converted to lowercase and vice versa. Example:
.capitalize
This method returns a new string with the first character converted to uppercase and the rest to lowercase. Example:
.strip
returns a new string with leading and trailing whitespace removed. For example:
.gsub
returns used for global substitution, replacing all occurrences of a pattern in a string with another string. For example:
Until next time, happy coding!
Top comments (0)