Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby String Translation

I want to find the successor of each element in my encoded string. For example K->M A->C etc.

string.each_char do |ch| 
    dummy_string<< ch.succ.succ
end

However this method translates y->aa.

Is there a method in Ruby that is like maketrans() in Python?

like image 433
unj2 Avatar asked Jul 14 '26 16:07

unj2


1 Answers

You seem to be looking for String#tr. Use like this: some_string.tr('a-zA-Z', 'c-zabC-ZAB')

like image 173
Pesto Avatar answered Jul 16 '26 05:07

Pesto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!