Quote (AbDuCt @ Sep 28 2014 11:58pm)
Well I guess that is correct, but the assignment called for a user to enter a phrase and then parse it character by character.
I guess
Code
char c[1024]; fgets(c, 1023, STDIN); while(*c) { printf("%c\n", c); }
or the like might work.
Well in all fairness, this exercise is pointless, as we could just as easily declare a function that wraps all that up. In which case, it would be reduced to a single "logical line" by invoking that function. That's what ruby is doing behind the scenes anyways. It isn't like ruby is magic. the implementation for each_char isn't a single line.
In fact, it is a hell of a lot more than a single line:
http://rxr.whitequark.org/mri/source/string.c#6474Code
static VALUE
6474 rb_str_enumerate_chars(VALUE str, int wantarray)
6475 {
6476 VALUE orig = str;
6477 VALUE substr;
6478 long i, len, n;
6479 const char *ptr;
6480 rb_encoding *enc;
6481 VALUE UNINITIALIZED_VAR(ary);
6482
6483 if (rb_block_given_p()) {
6484 if (wantarray) {
6485 #if STRING_ENUMERATORS_WANTARRAY
6486 rb_warn("given block not used");
6487 ary = rb_ary_new();
6488 #else
6489 rb_warning("passing a block to String#chars is deprecated");
6490 wantarray = 0;
6491 #endif
6492 }
6493 }
6494 else {
6495 if (wantarray)
6496 ary = rb_ary_new();
6497 else
6498 RETURN_SIZED_ENUMERATOR(str, 0, 0, rb_str_each_char_size);
6499 }
6500
6501 str = rb_str_new4(str);
6502 ptr = RSTRING_PTR(str);
6503 len = RSTRING_LEN(str);
6504 enc = rb_enc_get(str);
6505 switch (ENC_CODERANGE(str)) {
6506 case ENC_CODERANGE_VALID:
6507 case ENC_CODERANGE_7BIT:
6508 for (i = 0; i < len; i += n) {
6509 n = rb_enc_fast_mbclen(ptr + i, ptr + len, enc);
6510 substr = rb_str_subseq(str, i, n);
6511 if (wantarray)
6512 rb_ary_push(ary, substr);
6513 else
6514 rb_yield(substr);
6515 }
6516 break;
6517 default:
6518 for (i = 0; i < len; i += n) {
6519 n = rb_enc_mbclen(ptr + i, ptr + len, enc);
6520 substr = rb_str_subseq(str, i, n);
6521 if (wantarray)
6522 rb_ary_push(ary, substr);
6523 else
6524 rb_yield(substr);
6525 }
6526 }
6527 RB_GC_GUARD(str);
6528 if (wantarray)
6529 return ary;
6530 else
6531 return orig;
6532 }
This post was edited by Minkomonster on Sep 28 2014 11:13pm