Brian Adkins
10/28/2007 2:44:00 AM
On Oct 27, 10:09 pm, "David A. Black" <dbl...@rubypal.com> wrote:
> Hi --
>
> On Sun, 28 Oct 2007, Xavier Noria wrote:
> > On Oct 28, 2007, at 1:15 AM, Junkone wrote:
>
> >> Hello
> >> I have a date like 20070801 in a string. how do i change it to
> >> 2007/08/01 using regex
>
> > A solution with unpack:
>
> > irb(main):003:0> "20070801".unpack("a4a2a2").join("/")
> > => "2007/08/01"
>
> And here's one with scanf:
>
> >> "20070801".scanf("%4s%2s%2s").join('/')
> => "2007/08/01"
Yeah, but scanf is implemented in Ruby, so it's 25 times slower (at
least by a crude, quick benchmark) than unpack or insert :(