Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2392

Re: String#each_*slice* methods (like Enumerable#each_slice)

From Quintus <sutniuq@gmx.net>
Newsgroups comp.lang.ruby
Subject Re: String#each_*slice* methods (like Enumerable#each_slice)
Date 2011-04-06 13:42 -0500
Organization Service de news de lacave.net
Message-ID <4D9CB445.8000504@gmx.net> (permalink)
References <BANLkTinMv0-L01+pwiiLc0Gruk+56B1GeQ@mail.gmail.com>

Show all headers | View raw


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

Back to comp.lang.ruby | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

String#each_*slice* methods (like Enumerable#each_slice) "Aaron D. Gifford" <astounding@gmail.com> - 2011-04-06 12:52 -0500
  Re: String#each_*slice* methods (like Enumerable#each_slice) Quintus <sutniuq@gmx.net> - 2011-04-06 13:42 -0500
    Re: String#each_*slice* methods (like Enumerable#each_slice) "Aaron D. Gifford" <astounding@gmail.com> - 2011-04-06 13:49 -0500
      Re: String#each_*slice* methods (like Enumerable#each_slice) "Aaron D. Gifford" <astounding@gmail.com> - 2011-04-06 14:09 -0500
  Re: String#each_*slice* methods (like Enumerable#each_slice) 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-06 18:33 -0500

csiph-web