From: "Michael W. Ryder" <_mwryder@...> Date: 2007-06-19T10:05:05+09:00 Subject: Re: How to repeat string patterns in Ruby? 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.