Class: Bitary::Bitwarr

Inherits:
Object
  • Object
show all
Defined in:
lib/bitary/bitwarr.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_data, bpi: Bitary::LONG) ⇒ Bitwarr

Returns a new instance of Bitwarr.



7
8
9
10
11
# File 'lib/bitary/bitwarr.rb', line 7

def initialize(initial_data, bpi: Bitary::LONG)
  @bitsize = init_bitsize(initial_data, bpi)
  @array = init_array(initial_data, @bitsize, bpi)
  @bpi = bpi
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



41
42
43
# File 'lib/bitary/bitwarr.rb', line 41

def method_missing(method, *, &)
  @array.respond_to?(method) ? @array.send(method, *, &) : super
end

Instance Attribute Details

#bitsizeObject (readonly)

Returns the value of attribute bitsize.



5
6
7
# File 'lib/bitary/bitwarr.rb', line 5

def bitsize
  @bitsize
end

#bpiObject

Returns the value of attribute bpi.



5
6
7
# File 'lib/bitary/bitwarr.rb', line 5

def bpi
  @bpi
end

Instance Method Details

#[](bit_index) ⇒ Object



13
# File 'lib/bitary/bitwarr.rb', line 13

def [](bit_index) = @array[item_index(bit_index)]

#[]=(bit_index, value) ⇒ Object



15
16
17
# File 'lib/bitary/bitwarr.rb', line 15

def []=(bit_index, value)
  @array[item_index(bit_index)] = value
end

#bit_at(index) ⇒ Object



19
# File 'lib/bitary/bitwarr.rb', line 19

def bit_at(index) = (self[index] >> (@bpi - (index % @bpi) - 1)) & 0x1

#bit_at!(index) ⇒ Object



20
# File 'lib/bitary/bitwarr.rb', line 20

def bit_at!(index) = self[index] |= 2**(@bpi - (index % @bpi) - 1)

#each_byte(&proc) ⇒ Object



28
29
30
31
32
# File 'lib/bitary/bitwarr.rb', line 28

def each_byte(&proc)
  @array.each do |item|
    explode_item(item, Bitary::BYTE, @bpi, &proc)
  end
end

#respond_to_missing?(method, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/bitary/bitwarr.rb', line 45

def respond_to_missing?(method, include_all = false)
  @array.respond_to?(method, include_all) || super
end

#to_sObject



26
# File 'lib/bitary/bitwarr.rb', line 26

def to_s = @array.map { |item| to_binstr(item) }.join(' ')

#unbit_at!(index) ⇒ Object



22
23
24
# File 'lib/bitary/bitwarr.rb', line 22

def unbit_at!(index)
  self[index] &= ((2**@bpi) - 1 - (2**(@bpi - (index % @bpi) - 1)))
end