From: Just Another Victim of the Ambient Morality Date: 2007-06-19T11:30:20+09:00 Subject: Re: How to repeat string patterns in Ruby? "Michael W. Ryder" <_mwryder@worldnet.att.net> wrote in message news:NXFdi.99294$Sa4.77366@bgtnsc05-news.ops.worldnet.att.net... > Just Another Victim of the Ambient Morality wrote: >> I have some funky Python code that I'm trying to modify but it's >> formatted with spaces instead of tabs, making it impossible to change. >> Now, there may be many different ways to solve this problem but the first >> impulse I had was to write a small Ruby script to reformat the code. >> Now, a certain pattern came up in my solution that I have seen >> before. I wanted to do something like this: >> >> num_tabs = num_spaces / tab_width >> tabs = num_tabs.collect { "\t" }.join >> >> ...but I discovered that there is not collect method in the integer >> object. The best I could come up with was: >> >> tabs = '' >> num_tabs.times { tabs << "\t" } >> >> Is there a more succinct, more Ruby-esque way to do this? >> Thank you... >> >> >> > Couldn't you just use something like b = a.gsub(" ", "\t") where the > number of spaces is your tab size? This would avoid having count the > spaces. This is a good idea. Unfortunately for me, the actual code is more complicated than what I posted. I simplified it to emphasize the particular problem I wanted to solve. The real program deals with issues where, say, one line has four spaces while the next line has seven instead of a reasonable eight...