Class: Byte

Inherits:
Object
  • Object
show all
Defined in:
lib/byte_array/byte.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Byte

Returns a new instance of Byte.



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/byte_array/byte.rb', line 2

def initialize val
  @value = case val
  when Integer
    raise RangeError, "#{val} is too large for Byte" if val > 255
    val
  when String
    raise RangeError, "#{val} is too large for Byte" if val.length > 1
    val.unpack("C").first
  else
    raise TypeError, "Wrong type #{val.class} for Byte"
  end
end

Class Method Details

.[](obj) ⇒ Object



15
# File 'lib/byte_array/byte.rb', line 15

def self.[] obj;     obj.is_a?(Byte) ? obj : Byte.new(obj)    end

.to_procObject



16
# File 'lib/byte_array/byte.rb', line 16

def self.to_proc;    ->obj{self[obj]}                         end

Instance Method Details

#*(o) ⇒ Object



26
# File 'lib/byte_array/byte.rb', line 26

def * o;         @value * o.to_i       end

#+(o) ⇒ Object



24
# File 'lib/byte_array/byte.rb', line 24

def + o;         @value + o.to_i       end

#-(o) ⇒ Object



25
# File 'lib/byte_array/byte.rb', line 25

def - o;         @value - o.to_i       end

#/(o) ⇒ Object



27
# File 'lib/byte_array/byte.rb', line 27

def / o;         @value / o.to_i       end

#==(o) ⇒ Object



22
# File 'lib/byte_array/byte.rb', line 22

def == o;        o.to_i == @value      end

#coerce(o) ⇒ Object



23
# File 'lib/byte_array/byte.rb', line 23

def coerce o;    [o.to_i, @value]      end

#inspectObject



18
# File 'lib/byte_array/byte.rb', line 18

def inspect;     @value                end

#to_iObject



19
# File 'lib/byte_array/byte.rb', line 19

def to_i;        @value                end

#to_sObject



20
# File 'lib/byte_array/byte.rb', line 20

def to_s;        [@value].pack("C")    end

#to_strObject



21
# File 'lib/byte_array/byte.rb', line 21

def to_str;      to_s                  end