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
/*
* Document-method: each_char
* call-seq:
* str.each_char {|cstr| block } => str
*
* Passes each character in <i>str</i> to the given block.
*
* "hello".each_char {|c| print c, ' ' }
*
* <em>produces:</em>
*
* h e l l o
*/
static VALUE
rb_str_each_char(VALUE str)
{
int i, len, n;
const char *ptr;
RETURN_ENUMERATOR(str, 0, 0);
str = rb_str_new4(str);
ptr = RSTRING(str)->ptr;
len = RSTRING(str)->len;
for (i = 0; i < len; i += n) {
n = mbclen(ptr[i]);
rb_yield(rb_str_substr(str, i, n));
}
return str;
}