On 12-Aug-06, at 10:32 PM, snacktime wrote: > Ok almost there one more question on using tr. If the to_string is a > variable, what to do? In perl you would use eval because the > transliteration for the from_string and to_string are done at compile > time. Following is the perl code that produces the correct result, > and the ruby code which I can't get to produce the same output. Any > ideas on how to do this correctly in ruby? > > > $even_parity = '\0\201\202\3\204\5\6\207\210\11\12\213\14\215\216\17 > \220\21\22\223\24\225\226\27\30\231\232\33\234\35\36\237\240\41\42 > \243\44\ > 245\246\47\50\251\252\53\254\55\56\2570\261\2623\26456\267\2709\72 > \273\74\275\276\77\300AB\303D\305\306GH\311\312K\314MN\317P\321\322S > \324UV\3 > 27\330YZ\333\134\335\336_\140\341\342c\344ef\347\350ij\353l\355\356o > \360qr\363t\365\366wx\371\372\173\374\175\176\377\0\201\202\3\204\5 > \6\207\ > 210\11\12\213\14\215\216\17\220\21\22\223\24\225\226\27\30\231\232 > \33\234\35\36\237\240\41\42\243\44\245\246\47\50\251\252\53\254\55 > \56\2570\2 > 61\2623\26456\267\2709\72\273\74\275\276\77\300AB\303D\305\306GH\311 > \312K\314MN\317P\321\322S\324UV\327\330YZ\333\134\335\336_\140\341 > \342c\34 > 4ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175 > \176\377'; > > eval <<EDQ; > > sub setEvenParity { > my(\@s) = \@_; > foreach (\@s) { > tr/\\0-\\377/$even_parity/; > } > wantarray ? \@s : join '', \@s; > } > > EDQ > die $@ if $@; > > $out = setEvenParity('s'); > print $out; > > > @even_parity = '\0\201\202\3\204\5\6\207\210\11\12\213\14\215\216\17 > \220\21\22\223\24\225\226\27\30\231\232\33\234\35\36\237\240\41\42 > \243\44\ > 245\246\47\50\251\252\53\254\55\56\2570\261\2623\26456\267\2709\72 > \273\74\275\276\77\300AB\303D\305\306GH\311\312K\314MN\317P\321\322S > \324UV\3 > 27\330YZ\333\134\335\336_\140\341\342c\344ef\347\350ij\353l\355\356o > \360qr\363t\365\366wx\371\372\173\374\175\176\377\0\201\202\3\204\5 > \6\207\ > 210\11\12\213\14\215\216\17\220\21\22\223\24\225\226\27\30\231\232 > \33\234\35\36\237\240\41\42\243\44\245\246\47\50\251\252\53\254\55 > \56\2570\2 > 61\2623\26456\267\2709\72\273\74\275\276\77\300AB\303D\305\306GH\311 > \312K\314MN\317P\321\322S\324UV\327\330YZ\333\134\335\336_\140\341 > \342c\34 > 4ef\347\350ij\353l\355\356o\360qr\363t\365\366wx\371\372\173\374\175 > \176\377'; > > eval <<EDQ > > def setEvenParity(str) > str.tr("\\0-\\377",@even_parity) > end > > EDQ > > out = setEvenParity('s') > print out Have you tried the seemingly simple approach like this, which avoids eval: ratdog:~ mike$ irb --prompt simple >> @upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' => "ABCDEFGHIJKLMNOPQRSTUVWXYZ" >> def uc(s) >> s.tr('abcdefghijklmnopqrstuvwxyz', @upper_case) >> end => nil >> uc('banana') => "BANANA" >> @upper_case = 'abcdefghijklmnopqrstuvwxyz' => "abcdefghijklmnopqrstuvwxyz" >> uc('banana') => "banana" Maybe I am misunderstanding what you want to do... Hope this helps, Mike -- Mike Stok <mike / stok.ca> http://www.stok.ca/~mike/ The "`Stok' disclaimers" apply.