In Files

Parent

String

Monkey patching Class String to add bitwise operators. Behaviour matching Perl’s: perldoc.perl.org/perlop.html#Bitwise-String-Operators

Public Class Methods

hamming_dist(str1, str2) click to toggle source

Method to that returns the case senstive Hamming Distance between two strings. en.wikipedia.org/wiki/Hamming_distance

    # File bits.rb, line 34
34:   def self.hamming_dist(str1, str2)
35:     raise StringError, "Uneven string lengths: #{str1.length} != #{str2.length}" if str1.length != str2.length
36:     (str1 ^ str2 ).count("\x01-\xff")
37:   end

Public Instance Methods

&(str) click to toggle source

Method that performs bitwise AND operation where bits are copied if they exists in BOTH operands. If the operand sizes are different, the & operator methods acts as though the longer operand were truncated to the length of the shorter.

    # File bits.rb, line 43
43:   def &(str)
44:     new = ""
45: 
46:     (0 ... [self.length, str.length].min).each do |i|
47:       new << (self[i].ord & str[i].ord)
48:     end
49: 
50:     new
51:   end
^(str) click to toggle source

Method that performs bitwise XOR operation where bits are copied if they exists in ONE BUT NOT BOTH operands.

    # File bits.rb, line 77
77:   def ^(str)
78:     new = ""
79: 
80:     (0 ... [self.length, str.length].min).each do |i|
81:       new << (self[i].ord ^ str[i].ord)
82:     end
83: 
84:     new
85:   end
|(str) click to toggle source

Method that performs bitwise OR operation where bits are copied if they exists in EITHER operands. If the operand sizes differ, the shorter operand is extended with the terminal part of the longer operand.

    # File bits.rb, line 57
57:   def |(str)
58:     new = ""
59: 
60:     min = [self.length, str.length].min
61: 
62:     (0 ... min).each do |i|
63:       new << (self[i].ord | str[i].ord)
64:     end
65: 
66:     if self.length > str.length
67:       new << self[min ... self.length]
68:     elsif self.length < str.length
69:       new << str[min ... str.length]
70:     end
71: 
72:     new
73:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.