From: Jeremy Bopp Date: 2011-03-04T00:37:11+09:00 Subject: Re: Monkey patching class String to add bitwise operators On 3/3/2011 06:43, Martin Hansen wrote: > Sorry for the delayed answer, I have been away. > > I fail to see how I can fix my code. Adding chr doesnt do the trick > (same error): > > new << (self[i].ord ^ str[i].ord).chr You can't just call chr on the result if you want the character to be '0' or '1'. irb(main):001:0> '0'[0].ord ^ '1'[0].ord => 1 Like Ryan said, the ascii value for the character '1' is actually 49; however, you have the value of 1, the number rather than the string. You need to add 48 to the result of your operation if you want to actually get '1'. irb(main):002:0> ('0'[0].ord ^ '1'[0].ord) + 48 => 49 You can then call chr on that. irb(main):003:0> (('0'[0].ord ^ '1'[0].ord) + 48).chr => "1" Doing this is going to break your other tests though since most of them expect to be working with letters instead of numbers. Your problem appears to be with how you expect to represent "binary" strings vs. regular strings. You seem to want strings that look like '1011011' to actually be converted into bit strings before processing and then converted back to "binary" strings after. If you know beforehand that you're going to work with a "binary" string though, you can easily avoid all your extra work by using String#to_i and Fixnum#to_s. irb(main):004:0> '1011011'.to_i(2) => 91 irb(main):005:0> '1011011'.to_i(2).to_s(2) => "1011011" irb(main):006:0> '1011011'.to_i(2) ^ '0000100'.to_i(2) => 95 irb(main):007:0> ('1011011'.to_i(2) ^ '0000100'.to_i(2)).to_s(2) => "1011111" -Jeremy