How do we use digits method, chars method, and split method in Ruby?
1 min readMay 24, 2023
digits Method:
If you have a number we want this number in reverse order, then this method is helpful.
If we want to extract all the digits of an integer from right to left, the newly added Integer.
12345.digits(1000)
=> [345, 12]
12345.digits(100)
=> [45, 23, 1]
12345.digits
=> [5, 4, 3, 2, 1]
chars Method:
This method returns the charter to the array.
chars parse the underlying bytes to return the string’s characters.
chars is faster.
str1 = "Apple"
=> "Apple"
str2 = "Banana"
=> "Banana"
str1.chars
=> ["A", "p", "p", "l", "e"]
str2.chars
=> ["B", "a", "n", "a", "n", "a"]
split Method:
Ruby string using the built-in split method.
split(‘’) uses a regular expression to achieve the same.
The method will return an array of string characters.
"Hello world foo".split
=> ["Hello", "world", "foo"]
"first, second, third, fourth, fifth".split(",")
=> ["first", " second", " third", " fourth", " fifth"]
"first, second, third, fourth, fifth".split("//")
=> ["first, second, third, fourth, fifth"]