How to repeat string patterns in Ruby?

  • Thread starter Just Another Victim of the Ambient Morality
  • Start date
J

Just Another Victim of the Ambient Morality

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...
 
M

Michael W. Ryder

Just said:
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.
 
J

Just Another Victim of the Ambient Morality

Michael W. Ryder said:
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...
 
R

Robert Klemme

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...

$ expand --help
Usage: expand [OPTION]... [FILE]...
Convert tabs in each FILE to spaces, writing to standard output.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-i, --initial do not convert tabs after non blanks
-t, --tabs=NUMBER have tabs NUMBER characters apart, not 8
-t, --tabs=LIST use comma separated list of explicit tab positions
--help display this help and exit
--version output version information and exit

Report bugs to <[email protected]>.

:)

robert
 
J

Jano Svitok

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...

$ expand --help
Usage: expand [OPTION]... [FILE]...
Convert tabs in each FILE to spaces, writing to standard output.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-i, --initial do not convert tabs after non blanks
-t, --tabs=NUMBER have tabs NUMBER characters apart, not 8
-t, --tabs=LIST use comma separated list of explicit tab positions
--help display this help and exit
--version output version information and exit

Report bugs to <[email protected]>.

:)

Maybe this one would be better:

unexpand --help
Usage: unexpand [OPTION]... [FILE]...
Convert blanks in each FILE to tabs, writing to standard output.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-a, --all convert all blanks, instead of just initial blanks
--first-only convert only leading sequences of blanks (overrides -a)
-t, --tabs=N have tabs N characters apart instead of 8 (enables -a)
-t, --tabs=LIST use comma separated LIST of tab positions (enables -a)
--help display this help and exit
--version output version information and exit

Report bugs to <[email protected]>.

:)
 
J

Just Another Victim of the Ambient Morality

Jano Svitok said:
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...

$ expand --help
Usage: expand [OPTION]... [FILE]...
Convert tabs in each FILE to spaces, writing to standard output.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-i, --initial do not convert tabs after non blanks
-t, --tabs=NUMBER have tabs NUMBER characters apart, not 8
-t, --tabs=LIST use comma separated list of explicit tab positions
--help display this help and exit
--version output version information and exit

Report bugs to <[email protected]>.

:)

Maybe this one would be better:

unexpand --help
Usage: unexpand [OPTION]... [FILE]...
Convert blanks in each FILE to tabs, writing to standard output.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
-a, --all convert all blanks, instead of just initial blanks
--first-only convert only leading sequences of blanks (overrides -a)
-t, --tabs=N have tabs N characters apart instead of 8 (enables -a)
-t, --tabs=LIST use comma separated LIST of tab positions (enables -a)
--help display this help and exit
--version output version information and exit

Report bugs to <[email protected]>.

:)

All useful information, thank you...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top