chars()
public
Returns an enumerator that gives each
character in the string. If a block is given, it iterates over each character in the string.
"foo".chars.to_a
Show source
static VALUE
rb_str_each_char(VALUE str)
{
long i, len, n;
const char *ptr;
rb_encoding *enc;
RETURN_ENUMERATOR(str, 0, 0);
str = rb_str_new4(str);
ptr = RSTRING_PTR(str);
len = RSTRING_LEN(str);
enc = rb_enc_get(str);
for (i = 0; i < len; i += n) {
n = rb_enc_mbclen(ptr + i, ptr + len, enc);
rb_yield(rb_str_subseq(str, i, n));
}
return str;
}