On Saturday 09 July 2005 11:25 am, Luke Worth wrote:
> On Sun, 2005-07-10 at 00:06 +0900, Daniel Brockman wrote:
> > Why not use String#shift, which is O(1)?
>
> Because it doesn't exist :)
>
> irb(main):001:0> "aoeu".shift
> NoMethodError: undefined method `shift' for "aoeu":String
>         from (irb):1
>
> To the people suggesting regex, sorry I did think of that. However it
> won't work because of the problem: I want to convert all instances of
> dot-space into a single space, and all instances of space-dot into a
> single dot. All dots not followed or preceded by spaces (excluding
> newline) must be turned into newlines.
> For example, how would you solve the following:
> "bla.h+aoeu.++test.\n+.hello" (where + represents a space)
> i think it's hard.

Will this work? (I used + instead of space as in your example)

str = "bla.h+aoeu.++test.\n+.hello"
p str.gsub(/\.\+|\+\.|\./) { |s| 
  case s
  when '+.' then '.'
  when '.+' then '+'
  else "\n"
  end
}
==> "bla\nh+aoeu++test\n\n.hello"

-- 
-- Jim Weirich    jim / weirichhouse.org     http://onestepback.org
-----------------------------------------------------------------
"Beware of bugs in the above code; I have only proved it correct, 
not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)