method
each
Ruby latest stable (v2_5_5)
-
0 notes -
Class: String
- 1_8_6_287 (0)
- 1_8_7_72 (-2)
- 1_8_7_330 (0)
- 1_9_1_378
- 1_9_2_180
- 1_9_3_125
- 1_9_3_392
- 2_1_10
- 2_2_9
- 2_4_6
- 2_5_5
- 2_6_3
- What's this?
Related methods
- Class methods (3)
- new
- try_convert
- yaml_new (<= v1_9_1_378)
- Instance methods (168)
- <<
- <=>
- =~
- ==
- ===
- -@
- []
- []=
- *
- %
- +
- +@
- ascii_only?
- b
- block_scanf
- bytes
- bytesize
- byteslice
- capitalize
- capitalize!
- casecmp
- casecmp?
- center
- chars
- chomp
- chomp!
- chop
- chop!
- chr
- clear
- codepoints
- concat
- count
- crypt
- delete
- delete!
- delete_prefix
- delete_prefix!
- delete_suffix
- delete_suffix!
- downcase
- downcase!
- dump
- each (<= v1_8_7_330)
- each_byte
- each_char
- each_codepoint
- each_grapheme_cluster
- each_line
- empty?
- encode
- encode!
- encoding
- end_regexp (<= v1_8_7_330)
- end_with?
- eql?
- _expand_ch (<= v1_8_7_330)
- expand_ch_hash (<= v1_8_7_330)
- ext (<= v2_2_9)
- force_encoding
- freeze
- getbyte
- grapheme_clusters
- gsub
- gsub!
- hash
- hex
- include?
- index
- initialize_copy
- insert
- inspect
- intern
- is_binary_data? (<= v1_9_1_378)
- is_complex_yaml? (<= v1_9_1_378)
- iseuc
- isjis
- issjis
- isutf8
- jcount (<= v1_8_7_330)
- jlength (<= v1_8_7_330)
- jsize (<= v1_8_7_330)
- kconv
- length
- lines
- ljust
- lstrip
- lstrip!
- match
- match?
- mbchar? (<= v1_8_7_330)
- next
- next!
- oct
- ord
- original_succ (<= v1_8_7_330)
- original_succ! (<= v1_8_7_330)
- parse_csv
- partition
- pathmap (<= v2_2_9)
- pathmap_explode (<= v2_2_9)
- pathmap_partial (<= v2_2_9)
- pathmap_replace (<= v2_2_9)
- prepend
- pretty_print
- quote (<= v1_8_6_287)
- _regex_quote (<= v1_8_7_330)
- replace
- reverse
- reverse!
- rindex
- rjust
- rpartition
- rstrip
- rstrip!
- scan
- scanf
- scrub
- scrub!
- setbyte
- shellescape
- shellsplit
- size
- slice
- slice!
- split
- squeeze
- squeeze!
- start_with?
- strip
- strip!
- sub
- sub!
- succ
- succ!
- sum
- swapcase
- swapcase!
- to_c
- to_d
- toeuc
- to_f
- to_i
- tojis
- tolocale
- to_r
- to_s
- tosjis
- to_str
- to_sym
- toutf16
- toutf32
- toutf8
- to_yaml (<= v1_9_1_378)
- tr
- tr!
- tr_s
- tr_s!
- undump
- unicode_normalize
- unicode_normalize!
- unicode_normalized?
- unpack
- unpack1
- upcase
- upcase!
- upto
- valid_encoding?
= private
= protected
Method deprecated or moved
This method is deprecated or moved on the latest stable version. The last existing version (v1_8_7_330) is shown here.
These similar methods exist in v2_5_5:
- Array#each
- Dir#each
- IO#each
- Hash#each
- Range#each
- Struct#each
- GetoptLong#each
- Prime#each
- Set#each
- Shell::Cat#each
- Shell::Glob#each
- Shell::SystemCommand#each
- Shell::Filter#each
- Shell::Concat#each
- Shell::Tee#each
- Shell::Echo#each
- StringIO#each
- WIN32OLE#each
- Net::POP3#each
- REXML::QuickPath#each
- REXML::AttlistDecl#each
- REXML::Parent#each
- REXML::Attributes#each
- REXML::Elements#each
- REXML::XPath#each
- REXML::SyncEnumerator#each
- REXML::Light::Node#each
- REXML::Parsers::PullParser#each
- Rinda::Tuple#each
- Rinda::NotifyTemplateEntry#each
- Rinda::RingFinger#each
- WEBrick::CGI::Socket#each
- WEBrick::HTTPRequest#each
- WEBrick::HTTPResponse#each
- WEBrick::HTTPAuth::Htdigest#each
- WEBrick::HTTPAuth::Htpasswd#each
- YAML::DBM#each
- Zlib::GzipReader#each
- RSS::XML::Element#each
- CSV#each
- CSV::Row#each
- CSV::Table#each
- Enumerator#each
- Enumerator::Generator#each
- GDBM#each
- OpenSSL::ASN1::Constructive#each
- OpenSSL::Config#each
- Prime::PseudoPrimeGenerator#each
- Shell::Void#each
- ARGF#each
- Matrix#each
- Vector#each
- OpenSSL::Buffering#each
- DBM#each
- Gem::ConfigFile#each
- Gem::DependencyList#each
- Gem::Package::TarReader#each
- Gem::Specification#each
- Psych::Nodes::Node#each
- SDBM#each
- Gem::AvailableSet#each
- Gem::List#each
- Gem::Resolver::RequirementList#each
- Gem::SourceList#each
- ObjectSpace::WeakMap#each
- Gem::Resolver::Molinillo::DependencyGraph#each
- Gem::Resolver::Molinillo::DependencyGraph::Log#each
each(...)
public
Splits str using the supplied parameter as the record separator ($/ by default), passing each substring in turn to the supplied block. If a zero-length record separator is supplied, the string is split into paragraphs delimited by multiple successive newlines.
print "Example one\n" "hello\nworld".each {|s| p s} print "Example two\n" "hello\nworld".each('l') {|s| p s} print "Example three\n" "hello\n\n\nworld".each('') {|s| p s}
produces:
Example one "hello\n" "world" Example two "hel" "l" "o\nworl" "d" Example three "hello\n\n\n" "world"
Show source
/* * call-seq: * str.each(separator=$/) {|substr| block } => str * str.each_line(separator=$/) {|substr| block } => str * * Splits <i>str</i> using the supplied parameter as the record separator * (<code>$/</code> by default), passing each substring in turn to the supplied * block. If a zero-length record separator is supplied, the string is split * into paragraphs delimited by multiple successive newlines. * * print "Example one\n" * "hello\nworld".each {|s| p s} * print "Example two\n" * "hello\nworld".each('l') {|s| p s} * print "Example three\n" * "hello\n\n\nworld".each('') {|s| p s} * * <em>produces:</em> * * Example one * "hello\n" * "world" * Example two * "hel" * "l" * "o\nworl" * "d" * Example three * "hello\n\n\n" * "world" */ static VALUE rb_str_each_line(argc, argv, str) int argc; VALUE *argv; VALUE str; { VALUE rs; int newline; char *p = RSTRING(str)->ptr, *pend = p + RSTRING(str)->len, *s; char *ptr = p; long len = RSTRING(str)->len, rslen; VALUE line; if (rb_scan_args(argc, argv, "01", &rs) == 0) { rs = rb_rs; } RETURN_ENUMERATOR(str, argc, argv); if (NIL_P(rs)) { rb_yield(str); return str; } StringValue(rs); rslen = RSTRING(rs)->len; if (rslen == 0) { newline = '\n'; } else { newline = RSTRING(rs)->ptr[rslen-1]; } for (s = p, p += rslen; p < pend; p++) { if (rslen == 0 && *p == '\n') { if (*++p != '\n') continue; while (*p == '\n') p++; } if (RSTRING(str)->ptr < p && p[-1] == newline && (rslen <= 1 || rb_memcmp(RSTRING(rs)->ptr, p-rslen, rslen) == 0)) { line = rb_str_new5(str, s, p - s); OBJ_INFECT(line, str); rb_yield(line); str_mod_check(str, ptr, len); s = p; } } if (s != pend) { if (p > pend) p = pend; line = rb_str_new5(str, s, p - s); OBJ_INFECT(line, str); rb_yield(line); } return str; }