Class: SDL4R::SdlBinary

Inherits:
Object
  • Object
show all
Defined in:
lib/sdl4r/sdl_binary.rb

Overview

Represents a binary value.

This class was introduced to avoid the confusion between a Ruby String and a binary literal.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bytes) ⇒ SdlBinary

value: a String containing the bytes



33
34
35
# File 'lib/sdl4r/sdl_binary.rb', line 33

def initialize(bytes)
  @bytes = bytes
end

Instance Attribute Details

#bytesObject

Returns the value of attribute bytes.



30
31
32
# File 'lib/sdl4r/sdl_binary.rb', line 30

def bytes
  @bytes
end

Class Method Details

.decode64(s) ⇒ Object

Decodes the specified base-64 encoded string and returns a corresponding SdlBinary instance. s might not include the conventional starting and ending square brackets.



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/sdl4r/sdl_binary.rb', line 57

def self.decode64(s)
  s = s.delete("\n\r\t ")

  binary = Base64.decode64(s)
  
  if binary.empty? and not s.empty?
    raise ArgumentError, "bad binary literal"
  end

  return SdlBinary.new(binary)
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?



37
38
39
40
41
# File 'lib/sdl4r/sdl_binary.rb', line 37

def ==(o)
  return true if self.equal?(o)
  return false if not o.instance_of?(self.class)
  return self.bytes == o.bytes
end

#hashObject



45
46
47
# File 'lib/sdl4r/sdl_binary.rb', line 45

def hash
  return bytes.hash
end

#to_sObject

Returns the bytes base64-encoded.



50
51
52
# File 'lib/sdl4r/sdl_binary.rb', line 50

def to_s
  return Base64.encode64(bytes)
end