X-FeedAbuse: http://nntpfeed.proxad.net/abuse.pl feeded by 88.191.16.109 Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!nospam.fr.eu.org!talisker.lacave.net!lacave.net!not-for-mail From: Quintus Newsgroups: comp.lang.ruby Subject: Re: String#each_*slice* methods (like Enumerable#each_slice) Date: Wed, 6 Apr 2011 13:42:25 -0500 Organization: Service de news de lacave.net Lines: 37 Message-ID: <4D9CB445.8000504@gmx.net> References: NNTP-Posting-Host: bristol.highgroove.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: talisker.lacave.net 1302115362 24251 65.111.164.187 (6 Apr 2011 18:42:42 GMT) X-Complaints-To: abuse@lacave.net NNTP-Posting-Date: Wed, 6 Apr 2011 18:42:42 +0000 (UTC) In-Reply-To: X-Received-From: This message has been automatically forwarded from the ruby-talk mailing list by a gateway at comp.lang.ruby. If it is SPAM, it did not originate at comp.lang.ruby. Please report the original sender, and not us. Thanks! For more details about this gateway, please visit: http://blog.grayproductions.net/categories/the_gateway X-Mail-Count: 381052 X-Ml-Name: ruby-talk X-Rubymirror: Yes X-Ruby-Talk: <4D9CB445.8000504@gmx.net> Xref: x330-a1.tempe.blueboxinc.net comp.lang.ruby:2392 Am 06.04.2011 19:52, schrieb Aaron D. Gifford: > So now for the question. Is there a better way to accomplish > something similar? I'm not debating whether to do it as a monkey > patch or not--that's irrelevant to me. But is there a more efficient > way to slice up strings and iterate over fixed sized chunks? > > One alternative each_bslice implementation I tried used > str.bytes.to_a.map(&:chr).each_slice(x){|c| p c.join} but it was a bit > slower in benchmarks versus the str.scan method. > > Aaron out. > > Use Enumarators: ================================ irb(main):001:0> str = "ÄÄÄÖÖÖÜÜÜ" => "ÄÄÄÖÖÖÜÜÜ" irb(main):002:0> str.chars.each_slice(3){|x| p x} ["Ä", "Ä", "Ä"] ["Ö", "Ö", "Ö"] ["Ü", "Ü", "Ü"] => nil irb(main):003:0> str.bytes.each_slice(3){|x| p x} [195, 132, 195] [132, 195, 132] [195, 150, 195] [150, 195, 150] [195, 156, 195] [156, 195, 156] => nil irb(main):004:0> ================================ Vale, Marvin