Chain regex replacement

hi,
I want to make a chain replacement in my String object. For example
a => b
c => d
f => k

etc. Is there a way to make this replacement in one regex or something
similar? I want to write a custom upcase downcase method, as the normal
one doesnt support unicode (and ruby-unicode doesnt compile on
windows).

Thanks in advance
onur

Did you look at tr

"a big boy".tr("a-z", "A-Z")
=> "A BIG BOY"

regards,

Brian

···

On 29/10/05, onurturgay@gmail.com <onurturgay@gmail.com> wrote:

hi,
I want to make a chain replacement in my String object. For example
a => b
c => d
f => k

etc. Is there a way to make this replacement in one regex or something
similar? I want to write a custom upcase downcase method, as the normal
one doesnt support unicode (and ruby-unicode doesnt compile on
windows).

Thanks in advance
onur

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

onurturgay@gmail.com wrote:

hi,
I want to make a chain replacement in my String object. For example
a => b
c => d
f => k

etc. Is there a way to make this replacement in one regex or something
similar? I want to write a custom upcase downcase method, as the
normal one doesnt support unicode (and ruby-unicode doesnt compile on
windows).

Thanks in advance
onur

If you can make changes in one step, i.e. there is no change that will be changed later, you can use the block form of gsub:

str.gsub /[acf]/ do |m|
  case m[0]
    when ?a; "b"
    when ?c; "d"
    when ?f; "k"
    else raise "error"
  end
end

Kind regards

    robert

> hi,
> I want to make a chain replacement in my String object. For example
> a => b
> c => d
> f => k
>
> etc. Is there a way to make this replacement in one regex or something
> similar? I want to write a custom upcase downcase method, as the normal
> one doesnt support unicode (and ruby-unicode doesnt compile on
> windows).
>
> Thanks in advance
> onur
>
>
>

Did you look at tr

"a big boy".tr("a-z", "A-Z")
=> "A BIG BOY"

regards,

Brian

or even more like your example:

"hal".tr("za-y", "a-z")

=> "ibm"

cheers,

Brian

···

On 29/10/05, Brian Schröder <ruby.brian@gmail.com> wrote:

On 29/10/05, onurturgay@gmail.com <onurturgay@gmail.com> wrote:

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

thanks a lot.