1

i found a ruby encryption library which is using this function.

def process(text)  
  0.upto(text.length-1) {|i| text[i] = text[i] ^ round}  
  text  
end

in ruby 1.9.x it throws an error - undefined method ^' for "\x1A":String__

is there any work around in ruby 1.9.x?

after googling i came to know that "In Ruby 1.9 string[index] returns character not code of the character (like it was in 1.8)." (https://rails.lighthouseapp.com/projects/8994/tickets/3144-undefined-method-for-string-ror-234)

Thanks in advance

raidfive
  • 6,603
  • 1
  • 35
  • 32
Krishna Prasad Varma
  • 4,670
  • 5
  • 29
  • 41

1 Answers1

2

Try text[i].ord ^ round. See Getting an ASCII character code in Ruby using `?` (question mark) fails for more info.

Community
  • 1
  • 1
cam
  • 14,192
  • 1
  • 44
  • 29
  • TypeError: can't convert Fixnum into String from (irb):90:in `[]=' from (irb):90:in `block in irb_binding' from (irb):90:in `upto' from (irb):90 – Krishna Prasad Varma Feb 05 '11 at 04:47
  • @Krishnaprasad, in that case you might try running `to_s` on the result of `text[i] ^ round` – cam Feb 05 '11 at 23:16