gsub wrapper

Discussion in 'Ruby' started by Hector, Mar 2, 2006.

  1. Hector

    Hector Guest

    Help!

    How do I get $1, $2, and $3 to get interpolated in the following
    snippet?

    string = 'This is a test of the emergency'
    regexp = Regexp.new('(\W)+(\w\w)(\W)+')
    replacement = '$1->$2<-$3'
    user_wants_upcase=FALSE

    # Note: All variables above are set by the user of the program, so I
    don't know
    # what they will contain before runtime.

    string.gsub!(regexp) {
    return_value = string # Here, how do I get $1, $2, and $3 to
    interpolate?

    if (user_wants_uppercase)
    return_value.upcase! || return_value
    end

    return_value
    }

    I want return_value to be -> 'This ->is<- a test ->of<- the emergency'

    Note: I know that I can get it to work with the non-block version of
    gsub! but I need to use the block version.

    Thanks

    Hector

    --
    Posted via http://www.ruby-forum.com/.
     
    Hector, Mar 2, 2006
    #1
    1. Advertising

  2. Hector wrote:
    > Help!
    >
    > How do I get $1, $2, and $3 to get interpolated in the following
    > snippet?
    >
    > string = 'This is a test of the emergency'
    > regexp = Regexp.new('(\W)+(\w\w)(\W)+')
    > replacement = '$1->$2<-$3'


    If you have '"#{$1}->#{$2}<-#{$3}"', you can eval
    it in the block. This is probably a bad idea, though.

    To make this a bit safer, you would probably want the
    user to give you some kind of a sprintf-style string
    that you format inside the block. This might limit
    the user to strictly linear numbering or something.

    > user_wants_upcase=FALSE
    >
    > # Note: All variables above are set by the user of the program, so I
    > don't know
    > # what they will contain before runtime.
    >
    > string.gsub!(regexp) {
    > return_value = string # Here, how do I get $1, $2, and $3 to
    > interpolate?
    >
    > if (user_wants_uppercase)
    > return_value.upcase! || return_value
    > end
    >
    > return_value
    > }
    >
    > I want return_value to be -> 'This ->is<- a test ->of<- the emergency'
    >
    > Note: I know that I can get it to work with the non-block version of
    > gsub! but I need to use the block version.
    >
    > Thanks
    >
    > Hector



    E

    --
    Posted via http://www.ruby-forum.com/.
     
    E. Saynatkari, Mar 2, 2006
    #2
    1. Advertising

  3. On Mar 2, 2006, at 6:09 PM, Hector wrote:

    > Help!
    >
    > How do I get $1, $2, and $3 to get interpolated in the following
    > snippet?
    >
    > string = 'This is a test of the emergency'
    > regexp = Regexp.new('(\W)+(\w\w)(\W)+')
    > replacement = '$1->$2<-$3'
    > user_wants_upcase=FALSE
    >
    > # Note: All variables above are set by the user of the program, so I
    > don't know
    > # what they will contain before runtime.
    >
    > string.gsub!(regexp) {
    > return_value = string # Here, how do I get $1, $2, and $3 to
    > interpolate?
    >
    > if (user_wants_uppercase)
    > return_value.upcase! || return_value
    > end
    >
    > return_value
    > }
    >
    > I want return_value to be -> 'This ->is<- a test ->of<- the emergency'
    >
    > Note: I know that I can get it to work with the non-block version of
    > gsub! but I need to use the block version.
    >
    > Thanks
    >
    > Hector
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >


    assuming I understand your question:

    str = "#{$1}->#{$2}<-#{$3}"

    Alternatively:

    str = "#$1->#$2<-#$3"
     
    Logan Capaldo, Mar 2, 2006
    #3
  4. $1 is a global variable which is set to the value matched by the
    bracketed bits. You just treat it like any string.

    e.g. "The first parameter was #$1" or number = $2.to_f

    In gsub substitutions the letters '\N' in the substitution text, where
    N is a number from 1 to 9, will be replaced by the substituted
    sequence. If you use double quotes, you will need two backslashes
    because this is not a normal escape sequence.

    str = "We love Ruby"
    puts str.gsub(/We (\w+) \w+/, 'Why do we \1 it?')
    puts str.gsub(/(\w+)$/, "it because \\1 allows us to do cool stuff!")
     
    Timothy Goddard, Mar 3, 2006
    #4
  5. Hector wrote:
    > Help!
    >
    > How do I get $1, $2, and $3 to get interpolated in the following
    > snippet?
    >
    > string = 'This is a test of the emergency'
    > regexp = Regexp.new('(\W)+(\w\w)(\W)+')
    > replacement = '$1->$2<-$3'
    > user_wants_upcase=FALSE
    >
    > # Note: All variables above are set by the user of the program, so I
    > don't know
    > # what they will contain before runtime.
    >
    > string.gsub!(regexp) {
    > return_value = string # Here, how do I get $1, $2, and $3 to
    > interpolate?
    >
    > if (user_wants_uppercase)
    > return_value.upcase! || return_value
    > end
    >
    > return_value
    > }
    >
    > I want return_value to be -> 'This ->is<- a test ->of<- the emergency'
    >
    > Note: I know that I can get it to work with the non-block version of
    > gsub! but I need to use the block version.


    In your case you don't even need groups, because you can nicely anchor the
    expression on word boundaries:

    >> upcase=true

    => true
    >> 'This is a test of the emergency'.gsub(/\b\w\w\b/) {|m| "->" << (upcase

    ? m.upcase : m) << "<-"}
    => "This ->IS<- a test ->OF<- the emergency"

    Otherwise

    >> 'This is a test of the emergency'.gsub(/(\W)+(\w\w)(\W)+/) {|m|

    "#$1->#{upcase ? $2.upcase : $2}<-#$3"}
    => "This ->IS<- a test ->OF<- the emergency"

    Note: #$1 in a double quoted string is a shortcut for #{$1}.

    Kind regards

    robert
     
    Robert Klemme, Mar 3, 2006
    #5
    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. Paul Rubel

    gsub(/\s*$/, "") doubling string

    Paul Rubel, Jul 21, 2003, in forum: Ruby
    Replies:
    2
    Views:
    125
    Yukihiro Matsumoto
    Jul 22, 2003
  2. Florian Gross
    Replies:
    8
    Views:
    148
    Yukihiro Matsumoto
    Aug 18, 2003
  3. Kurt Euler
    Replies:
    3
    Views:
    101
    Mauricio Fernández
    Sep 4, 2003
  4. aurelianito

    gsub and gsub! are inconsistent

    aurelianito, Nov 8, 2005, in forum: Ruby
    Replies:
    9
    Views:
    183
    Robert Klemme
    Nov 9, 2005
  5. Hector

    gsub wrapper re-submitted

    Hector, Mar 2, 2006, in forum: Ruby
    Replies:
    0
    Views:
    114
    Hector
    Mar 2, 2006
Loading...

Share This Page