Class: DHT::Key
- Inherits:
-
Object
- Object
- DHT::Key
- Defined in:
- lib/dht/key.rb
Constant Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #==(that) ⇒ Object
- #distance_to(that) ⇒ Object
- #eql?(that) ⇒ Boolean
- #hash ⇒ Object
-
#initialize(key) ⇒ Key
constructor
A new instance of Key.
- #inspect ⇒ Object
- #to_binary ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(key) ⇒ Key
Returns a new instance of Key.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/dht/key.rb', line 12 def initialize(key) @key = if Key === key key.to_binary elsif Integer === key @key_i = key k = @key_i.to_s(16) [('0' * ((Size * 2) - k.size)) + k].pack('H*') else key = key.to_s case key.size when Size then key when Size * 2 then [key].pack('H*') else raise "Invalid key: #{key}" end end end |
Class Method Details
Instance Method Details
#==(that) ⇒ Object
30 31 32 |
# File 'lib/dht/key.rb', line 30 def ==(that) self.eql? that end |
#distance_to(that) ⇒ Object
62 63 64 |
# File 'lib/dht/key.rb', line 62 def distance_to(that) self.to_i ^ that.to_i end |
#eql?(that) ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/dht/key.rb', line 46 def eql?(that) if Key === that @key == that.to_binary else @key.to_s == that.to_s end end |
#hash ⇒ Object
42 43 44 |
# File 'lib/dht/key.rb', line 42 def hash @key.hash end |
#inspect ⇒ Object
54 55 56 |
# File 'lib/dht/key.rb', line 54 def inspect to_s end |
#to_binary ⇒ Object
34 35 36 |
# File 'lib/dht/key.rb', line 34 def to_binary @key end |
#to_i ⇒ Object
58 59 60 |
# File 'lib/dht/key.rb', line 58 def to_i @key_i ||= eval( '0x' + self.to_s ) end |
#to_s ⇒ Object
38 39 40 |
# File 'lib/dht/key.rb', line 38 def to_s to_binary.unpack('H*').first end |