From: Robert Klemme Date: 2008-08-15T21:57:18+09:00 Subject: Re: Idiom of removing a particular character from a String? 2008/8/14 Lorenzo E. Danielsson : > On Thu, 2008-08-14 at 23:52 +0900, Michael Libby wrote: >> On Thu, Aug 14, 2008 at 9:21 AM, Lorenzo E. Danielsson > The "problem" is that slice does not return the resulting string but > rather the sliced portion. This is of course not a problem in itself, it > is rather the expected behavior of slice. But, in my particular case > that *still* leads to: > > str = "hello" > rem = str.clone > rem.slice!(2) > rem # => "helo" > > Whereas I would have wanted: > > str = "hello" > rem = str.slice_and_return_everything_but_the_sliced_part(2) > > This is partly a matter of laziness on my part, that I prefer to type > two lines instead of four. But, it is also a matter that it kind of > breaks the POLS, at least for me. I have grown accustomed to being able > to perform: > > result = original.some_method(args) > > and have the result be some mutilated form of original, with original > being left intact. So when String#delete_at(index) doesn't work, it > feels like it *should* have worked.. > > Anyways, nothing worth losing sleep over.. If you want to do it on one line you could do this: irb(main):011:0> s = "abcdef" => "abcdef" irb(main):012:0> 5.times {|i| puts s.sub /\A(.{#{i}})./, '\\1' } bcdef acdef abdef abcef abcdf => 5 irb(main):013:0> Granted, it's not too idiomatic... Kind regards robert -- use.inject do |as, often| as.you_can - without end