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


Groups > comp.lang.ruby > #2416

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

From 7stud -- <bbxx789_05ss@yahoo.com>
Newsgroups comp.lang.ruby
Subject Re: String#each_*slice* methods (like Enumerable#each_slice)
Date 2011-04-06 18:33 -0500
Organization Service de news de lacave.net
Message-ID <14aa0a86278a4755ef0050004fb530c9@ruby-forum.com> (permalink)
References <BANLkTinMv0-L01+pwiiLc0Gruk+56B1GeQ@mail.gmail.com>

Show all headers | View raw


Aaron D. Gifford wrote in post #991274:
> Hi,
>
> I find I periodically need to iterate over slices of a string.
> Enumerable has the useful each_slice method, but in Ruby 1.9, I don't
> see an equivalent for the String class.
>

How about:

str = "hello world"

while str.size > 0
  substr = str.slice!(0, 3)  #(offset, length)
  puts "-->#{substr}<--"
end

--output:--
-->hel<--
-->lo <--
-->wor<--
-->ld<--

-- 
Posted via http://www.ruby-forum.com/.

Back to comp.lang.ruby | Previous | NextPrevious 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