From: Robert Klemme Date: 2008-01-29T17:36:59+09:00 Subject: Re: Logical Operations on Strings 2008/1/28, Michael W. Ryder <_mwryder@worldnet.att.net>: > Robert Klemme wrote: > > I believe this implementation is flawed because it returns a string > > that is too long if self.size > m.size. > > Actually I designed it that way so that I could change only part of the > original string without having to create a mask of 0xFF for the > remaining characters. Ah, ok, then this was on purpose. Personally I'd consider an element that is not there as 0x00 for OR and AND - that's why I did it this way. > Your implementations look a little cleaner than > my first try and I will probably use them in the finished version. I > still wonder why these weren't implemented in Ruby as I have been using > them for over 25 years in other languages. Since Ruby is so flexible chances are that you will find an equally easy (or easier) way. :-) > The reason I asked is that sometimes it is easier for me to think in hex > when creating masks for logical operations. Plus, I learned the PCL > codes for printing using hex and it is easier for me to use them that > way. In Business Basic to enter a group of hex characters into a string > I just use A$=$1B2C062C$ to create a 4 character string. No need to > enter each character individually. Then #pack and #unpack are your friends (see Adam's reply). You could easily put that in a method if you want a more concise representation, e.g. def h(s) [s.to_str].pack 'H*' end irb(main):005:0> h '212223' => "!\"#" irb(main):006:0> h '414243' => "ABC" irb(main):010:0> h %Q{414243} => "ABC" irb(main):011:0> h %q{414243} => "ABC" Note also, that you can use hex constants in Ruby: irb(main):001:0> 0x7b => 123 but it seems you are more after construction of strings. Kind regards robert -- use.inject do |as, often| as.you_can - without end