From: Florian Gross Date: 2005-03-24T08:24:52+09:00 Subject: Re: Any guides for good coding in Ruby? Josef 'Jupp' Schugt wrote: > Here is the HOW IT IS DONE: > > def threepack(x) > return x unless x.class == String > if /^[-+]?\d+\.\d+$/.match(x) > match = /((\d\d\d)+)(?=\.)/.match(x) > elsif /^[-+]?\d+$/.match(x) > match = /((\d\d\d)+)$/.match(x) > end > return x if match.nil? > result = match.pre_match > temp = match[1] > until temp.empty? > result << ',' unless ['', '-', '+'].member?(result) > result << temp[0..2] > temp = temp[3..-1] > end > result << match.post_match unless match.post_match.nil? > result > end Nifty code, though I wonder if it could be simplified by using String#rindex? It's a nice method that's not commonly used, but very nifty when needing to work from the end to the beginning.