split a string into an array of characters in ruby

To split a string into an array of characters in Ruby, you can use the chars method which returns an array of characters.

main.rb
string = "hello"
array = string.chars
puts array.inspect
# Output: ["h", "e", "l", "l", "o"]
93 chars
5 lines

You can also use the split method to split a string into an array of characters by passing an empty string as a separator.

main.rb
string = "hello"
array = string.split('')
puts array.inspect
# Output: ["h", "e", "l", "l", "o"]
97 chars
5 lines

Both methods will result in the same output, which is an array of characters that make up the original string.

gistlibby LogSnag