How to repeat string patterns in Ruby?

Discussion in 'Ruby' started by Just Another Victim of the Ambient Morality, Jun 19, 2007.

  1. 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...
    Just Another Victim of the Ambient Morality, Jun 19, 2007
    #1
    1. Advertising

  2. 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.
    Michael W. Ryder, Jun 19, 2007
    #2
    1. Advertising

  3. Just Another Victim of the Ambient Morality

    Mark Day Guest

    On Jun 18, 2007, at 5:55 PM, Just Another Victim of the Ambient
    Morality wrote:

    > tabs = ''
    > num_tabs.times { tabs << "\t" }
    >
    > Is there a more succinct, more Ruby-esque way to do this?


    "\t" * num_tabs

    -Mark
    Mark Day, Jun 19, 2007
    #3
  4. "Mark Day" <> wrote in message
    news:...
    > On Jun 18, 2007, at 5:55 PM, Just Another Victim of the Ambient Morality
    > wrote:
    >
    >> tabs = ''
    >> num_tabs.times { tabs << "\t" }
    >>
    >> Is there a more succinct, more Ruby-esque way to do this?

    >
    > "\t" * num_tabs


    Thank you, this is exactly what I am looking for!
    Just Another Victim of the Ambient Morality, Jun 19, 2007
    #4
  5. "Michael W. Ryder" <> wrote in message
    news:NXFdi.99294$...
    > 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...
    Just Another Victim of the Ambient Morality, Jun 19, 2007
    #5
  6. On 19.06.2007 02:53, 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...


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

    :)

    robert
    Robert Klemme, Jun 19, 2007
    #6
  7. Just Another Victim of the Ambient Morality

    Jano Svitok Guest

    On 6/19/07, Robert Klemme <> wrote:
    > On 19.06.2007 02:53, 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...

    >
    > $ 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 <>.
    >
    > :)


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

    :)
    Jano Svitok, Jun 20, 2007
    #7
  8. "Jano Svitok" <> wrote in message
    news:...
    > On 6/19/07, Robert Klemme <> wrote:
    >> On 19.06.2007 02:53, 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...

    >>
    >> $ 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 <>.
    >>
    >> :)

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


    All useful information, thank you...
    Just Another Victim of the Ambient Morality, Jul 11, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thorsten
    Replies:
    1
    Views:
    432
  2. Ashton

    String patterns in schema

    Ashton, Feb 11, 2004, in forum: XML
    Replies:
    0
    Views:
    385
    Ashton
    Feb 11, 2004
  3. crichmon
    Replies:
    4
    Views:
    470
    Mabden
    Jul 7, 2004
  4. Zoe Phoenix

    repeat a method inside a string

    Zoe Phoenix, Jul 19, 2008, in forum: Ruby
    Replies:
    3
    Views:
    112
    Tachikoma
    Jul 20, 2008
  5. Csaba  Gabor
    Replies:
    8
    Views:
    115
    Richard Cornford
    May 10, 2006
Loading...

Share This Page