Class: Delta

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

Class Method Summary collapse

Class Method Details

.asci_decode(array_of_digits) ⇒ Object



25
26
27
# File 'lib/deltacoding.rb', line 25

def asci_decode(array_of_digits)
  decode(array_of_digits).map { |e| e.chr(Encoding::UTF_8) }.join
end

.asci_encode(string) ⇒ Object



21
22
23
# File 'lib/deltacoding.rb', line 21

def asci_encode(string)
  encode(string.split('').map(&:ord))
end

.decode(array_of_digits) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/deltacoding.rb', line 13

def decode(array_of_digits)
  array_of_digits.each.with_index.each_with_object([]) do |(elem, index), arr|
    next arr << elem if index.zero?

    arr << elem + arr[index - 1]
  end
end

.encode(array_of_digits) ⇒ Object



5
6
7
8
9
10
11
# File 'lib/deltacoding.rb', line 5

def encode(array_of_digits)
  array_of_digits.map.with_index do |elem, index|
    next elem if index.zero?

    elem - array_of_digits[index - 1]
  end
end