method
lines
v1_9_1_378 -
Show latest stable
- Class:
String
lines(p1 = v1)public
Returns an enumerator that gives each line in the string. If a block is given, it iterates over each line in the string.
"foo\nbar\n".lines.to_a #=> ["foo\n", "bar\n"] "foo\nb ar".lines.sort #=> ["b ar", "foo\n"]
static VALUE
rb_str_each_line(int argc, VALUE *argv, VALUE str)
{
rb_encoding *enc;
VALUE rs;
unsigned int newline;
char *p, *pend, *s, *ptr;
long len, rslen;
VALUE line;
int n;
VALUE orig = str;
if (argc == 0) {
rs = rb_rs;
}
else {
rb_scan_args(argc, argv, "01", &rs);
}
RETURN_ENUMERATOR(str, argc, argv);
if (NIL_P(rs)) {
rb_yield(str);
return orig;
}
str = rb_str_new4(str);
ptr = p = s = RSTRING_PTR(str);
pend = p + RSTRING_LEN(str);
len = RSTRING_LEN(str);
StringValue(rs);
if (rs == rb_default_rs) {
enc = rb_enc_get(str);
while (p < pend) {
char *p0;
p = memchr(p, '\n', pend - p);
if (!p) break;
p0 = rb_enc_left_char_head(s, p, pend, enc);
if (!rb_enc_is_newline(p0, pend, enc)) {
p++;
continue;
}
p = p0 + rb_enc_mbclen(p0, pend, enc);
line = rb_str_new5(str, s, p - s);
OBJ_INFECT(line, str);
rb_enc_cr_str_copy_for_substr(line, str);
rb_yield(line);
str_mod_check(str, ptr, len);
s = p;
}
goto finish;
}
enc = rb_enc_check(str, rs);
rslen = RSTRING_LEN(rs);
if (rslen == 0) {
newline = '\n';
}
else {
newline = rb_enc_codepoint(RSTRING_PTR(rs), RSTRING_END(rs), enc);
}
while (p < pend) {
unsigned int c = rb_enc_codepoint(p, pend, enc);
again:
n = rb_enc_codelen(c, enc);
if (rslen == 0 && c == newline) {
p += n;
if (p < pend && (c = rb_enc_codepoint(p, pend, enc)) != newline) {
goto again;
}
while (p < pend && rb_enc_codepoint(p, pend, enc) == newline) {
p += n;
}
p -= n;
}
if (c == newline &&
(rslen <= 1 || memcmp(RSTRING_PTR(rs), p, rslen) == 0)) {
line = rb_str_new5(str, s, p - s + (rslen ? rslen : n));
OBJ_INFECT(line, str);
rb_enc_cr_str_copy_for_substr(line, str);
rb_yield(line);
str_mod_check(str, ptr, len);
s = p + (rslen ? rslen : n);
}
p += n;
}
finish:
if (s != pend) {
line = rb_str_new5(str, s, pend - s);
OBJ_INFECT(line, str);
rb_enc_cr_str_copy_for_substr(line, str);
rb_yield(line);
}
return orig;
} Related methods
- Instance methods
- %
- *
- +
- <<
- <=>
- ==
- ===
- =~
- []
- []=
- ascii_only?
- block_scanf
- bytes
- bytesize
- capitalize
- capitalize!
- casecmp
- center
- chars
- chomp
- chomp!
- chop
- chop!
- chr
- clear
- codepoints
- concat
- count
- crypt
- delete
- delete!
- downcase
- downcase!
- dump
- each_byte
- each_char
- each_codepoint
- each_line
- empty?
- encode
- encode!
- encoding
- end_with?
- eql?
- force_encoding
- getbyte
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- is_binary_data?
- is_complex_yaml?
- iseuc
- isjis
- issjis
- isutf8
- kconv
- length
- lines
- ljust
- lstrip
- lstrip!
- match
- next
- next!
- oct
- ord
- parse_csv
- partition
- replace
- reverse
- reverse!
- rindex
- rjust
- rpartition
- rstrip
- rstrip!
- scan
- scanf
- setbyte
- shellescape
- shellsplit
- size
- slice
- slice!
- split
- squeeze
- squeeze!
- start_with?
- strip
- strip!
- sub
- sub!
- succ
- succ!
- sum
- swapcase
- swapcase!
- to_c
- to_d
- to_f
- to_i
- to_r
- to_s
- to_str
- to_sym
- to_yaml
- toeuc
- tojis
- tolocale
- tosjis
- toutf16
- toutf32
- toutf8
- tr
- tr!
- tr_s
- tr_s!
- unpack
- upcase
- upcase!
- upto
- valid_encoding?
- Class methods
- new
- try_convert
- yaml_new