Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
String#chars
() -> ::Array[::String]
() { (::String char) -> void } -> self
chars → array_of_characters
string.c
View on GitHub ↗
static VALUE
rb_str_chars(VALUE str)
{
VALUE ary = WANTARRAY("chars", rb_str_strlen(str));
return rb_str_enumerate_chars(str, ary);
}
Returns an array of the characters in self:
Example
'hello'.chars # => ["h", "e", "l", "l", "o"] 'こんにちは'.chars # => ["こ", "ん", "に", "ち", "は"] ''.chars # => []
Related: see Converting to Non-String.