From: Harry Kakueki Date: 2007-05-04T14:11:48+09:00 Subject: Re: Is there a better way to do this? On 5/4/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote: > I am trying to come up with a "generic" formatting routine so that I > could feed it something like sform("123456789", "000-00-0000") or > sform("1234567890", "(000) 000-0000") or sform("123456", "00/00/00") and > it would work. I could easily do something like you suggest, and for > some cases it might be better, but I want something I can for any number > of formats. Thanks for the input. > > I was thinking about your question again and came up with this. Just before I posted I saw that William had taken a similar approach and offered it in a more compact form. Anyway , for what it's worth. I was thinking the dots should only be in the formatting, not in the input so I stripped them out of the input. #arr = ["123456", "00/00/00"] arr = ["12345.67", "$00,000.00"] #arr = ["1234567890", "(000) 000-0000"] inp_arr = arr[0].delete(".").split(//) fmt_arr = arr[1].split(//) str = "" fmt_arr.each do |x| str << inp_arr.shift if x =~ /\d/ str << x if x !~ /\d/ end p str Harry -- http://www.kakueki.com/ruby/list.html A Look into Japanese Ruby List in English