From: Nobuyoshi Nakada <nobu@...>
Date: 2011-04-27T23:55:49+09:00
Subject: [ruby-core:35931] [Ruby 1.9 - Feature #4615] Add IO#split and iterative form of String#split


Issue #4615 has been updated by Nobuyoshi Nakada.


It should be another feature.
----------------------------------------
Feature #4615: Add IO#split and iterative form of String#split
http://redmine.ruby-lang.org/issues/4615

Author: Joey Zhou
Status: Rejected
Priority: Normal
Assignee: Yukihiro Matsumoto
Category: 
Target version: 


file.each_line(sep=$/) {|line| ... } can be used to iterate on lines.

But the separator is just a string or nil, it cannot be a regexp.

Sometimes I may want to iterate on "sentences", which are strings separated by (simply say) punctuations ".;!?".

So if I can write it like this:

  file.split(/[.;!?]/) {|sentence| ... }

I think it will be very convenient.

You may say I can write it like this:

  file.gets(nil).split(/[.;!?]/).each {|sentence| ... }

But this code will: (1) slurp in the whole file; (2) create a temporary array. It the file is a big one, those 2 steps seem both expensive and unnessary.

So I suggest a flexible IO#split: (also available for File and ARGF)

  io.split(pattern=$/) {|field|...} -> io # default pattern is $/, not $;
  io.split(pattern=$/) -> enumerator # not array

(I think adding a new method is better, rather than modifying the IO#each_line, making it accept regexp as argument.)

Well, String#split has only one form:

  str.split(pattern=$;, limit=0) -> array

Maybe add a iterative form, when with a block:

  str.split(pattern=$;, limit=0) {|field| ... } -> str


Joey Zhou


-- 
http://redmine.ruby-lang.org