[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

,elegant

Leslie Viljoen

12/13/2005 5:25:00 PM

Hiya!

This function adds commas to a string after every three characters
so a number would be easier to read. It's kinda ugly though, and I can't
help thinking there'd be a elegant Why-esque one or two liner to do
this kind of thing - anyone care to improve it?


def add_commas(str)
return nil if str.class != String

ret = ""
len = str.length
i = len - 1
while i >= 0
ret = str[i].chr + ret
if ((len-i) % 3) == 0
ret = "," + ret
end
i -= 1
end
if ret[0].chr == ","
ret = ret[1..-1]
end
ret
end




4 Answers

James Gray

12/13/2005 5:39:00 PM

0

On Dec 13, 2005, at 11:24 AM, Leslie Viljoen wrote:

> Hiya!
>
> This function adds commas to a string after every three characters
> so a number would be easier to read. It's kinda ugly though, and I
> can't
> help thinking there'd be a elegant Why-esque one or two liner to do
> this kind of thing - anyone care to improve it?

Does this help?

http://rubyu...

James Edward Gray II


Leslie Viljoen

12/13/2005 5:46:00 PM

0

James Edward Gray II wrote:

> On Dec 13, 2005, at 11:24 AM, Leslie Viljoen wrote:
>
>> Hiya!
>>
>> This function adds commas to a string after every three characters
>> so a number would be easier to read. It's kinda ugly though, and I
>> can't
>> help thinking there'd be a elegant Why-esque one or two liner to do
>> this kind of thing - anyone care to improve it?
>
>
> Does this help?
>
> http://rubyu...
>
> James Edward Gray II
>
I knew it! This kind of thing makes me crazy! I used to think I was a
good programmer!




Ara.T.Howard

12/13/2005 5:58:00 PM

0

www.freedomtofascism.com

6/23/2007 10:31:00 PM

0

On Sat, 23 Jun 2007 20:09:05 GMT, gary.wallace@mciter.com wrote:

>Really? What technology is this?

A dash of Tesla and a scosh of Russian. :->

Russia to reform national economy to get rid of oil dependence

British scientists recently released a report warning that world oil
supplies could run out faster than expected. The scientists seem to have
been commissioned by the White House to come up with a forecast that
justifies U.S. aggressive policies toward oil-exporting countries. However,
Russians have no reasons to panic even if the prediction proves correct
since Russia is going to rid itself of the dependence on oil by reforming
economy.

More:

http://english.pravda.ru/russia/economics/22-06-2007/93835-ru...





--
Posted via a free Usenet account from http://www.te...