rencdec 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +31 -0
  2. data/lib/rencdec.rb +24 -0
  3. metadata +62 -0
@@ -0,0 +1,31 @@
1
+ = Rencdec
2
+ Ruby encoder/decoder for arrays of arbitrary elements.
3
+
4
+ == Purpose
5
+ Created to fill a need in supplying two arrays (a,b) of varying size and representing a third array (c) that uses items from the first array (a) and reversably encodes it into items from the second array (b) and returns the fourth array (d). Decoding using the same arrays should turn fourth array (d) into the third array (c). Examples include letters to numbers, musical notes to colors, or any other grouping of objects describable as an array words.
6
+
7
+ === Example A
8
+ Simple letters to numbers.
9
+ data = 'hello'.chars.map(&:to_sym) #=> [:h, :e, :l, :l, :o]
10
+ current = abcdefghijklmnopqrstuvwxyz.chars.map(&:to_sym) #=> [:a, :b, :c, :d, :e, :f, :g, :h, :i, :j, :k, :l, :m, :n, :o, :p, :q, :r, :s, :t, :u, :v, :w, :x, :y, :z]
11
+ target = (0..9).to_a #=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
12
+ rencdec = Rencdec.new(current_data: data, current_encoding: current, target_encoding: target)
13
+ rencdec.encode #=> [3, 2, 7, 6, 8, 7, 2]
14
+ rencdec.decode #=> [:h, :e, :l, :l, :o]
15
+ rencdec.verify #=> true
16
+ === Example B
17
+ Arbitrary letters to other letters.
18
+ rencdec = Rencdec.new(current_data: [:q,:w,:e,:r,:t,:y], current_encoding: [:e,:q,:r,:t,:w,:y], target_encoding: [:a,:b,:c,:d])
19
+ rencdec.encode #=> [:d, :a, :c, :d, :d, :d, :d]
20
+ rencdec.decode #=> [:q, :w, :e, :r, :t, :y]
21
+ rencdec.verify #=> true
22
+ === Example C
23
+ Musical notes to colors.
24
+ data = [:c,:d,:e,:f,:g,:a,:b,:c]
25
+ current = [:c,:cs,:d,:ds,:e,:f,:fs,:g,:gs,:a,:as,:b]
26
+ target = %w{red violet blue green yellow orange}.map(&:to_sym) #=> [:red, :violet, :blue, :green, :yellow, :orange]
27
+ rencdec = Rencdec.new(current_data: data, current_encoding: current, target_encoding: target)
28
+ rencdec.encode #=> [:yellow, :violet, :violet, :orange, :red, :blue, :green, :yellow, :red]
29
+ rencdec.decode #=> [:c, :d, :e, :f, :g, :a, :b, :c]
30
+ rencdec.verify #=> true
31
+ = /Rencdec
@@ -0,0 +1,24 @@
1
+ require 'radix'
2
+
3
+ class Rencdec
4
+ attr_reader :verbose, :current_encoding, :target_encoding, :current_data, :current_radix, :target_radix, :target_data
5
+ def initialize(params)
6
+ @current_encoding, @target_encoding, @current_data = params[:current_encoding], params[:target_encoding], params[:current_data]
7
+
8
+ @current_radix = Radix::Base.new current_encoding
9
+ @target_radix = Radix::Base.new target_encoding
10
+ end
11
+ def encode
12
+ tmp_current = current_data.map{|c| current_radix.values[c.to_s]}
13
+ tmp_target = Radix.convert_base(tmp_current, current_radix.base, target_radix.base)
14
+ @target_data ||= tmp_target.map{|t| tmp = target_radix.values.invert[t]; tmp[/^\d+$/] ? tmp.to_i : tmp.to_sym }
15
+ end
16
+ def decode
17
+ tmp_target = target_data.map{|t| target_radix.values[t.to_s]}
18
+ tmp_current = Radix.convert_base(tmp_target, target_radix.base, current_radix.base)
19
+ @current_data ||= tmp_current.map{|t| tmp = current_radix.values.invert[t]; tmp[/^\d+$/] ? tmp.to_i : tmp.to_sym }
20
+ end
21
+ def verify
22
+ encode == target_data && decode == current_data
23
+ end
24
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rencdec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - klappy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-04-13 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: radix
16
+ requirement: &70232482048260 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70232482048260
25
+ description: Created to fill a need in supplying two arrays (a,b) of varying size
26
+ and representing a third array (c) that uses items from the first array (a) and
27
+ reversably encodes it into items from the second array (b) and returns the fourth
28
+ array (d). Decoding using the same arrays should turn fourth array (d) into the
29
+ third array (c). Examples include letters to numbers, musical notes to colors, or
30
+ any other grouping of objects describable as an array words.
31
+ email: christopher@klapp.name
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/rencdec.rb
37
+ - README.rdoc
38
+ homepage: https://github.com/klappy/rencdec
39
+ licenses: []
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.10
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: Ruby encoder/decoder for arrays of arbitrary elements.
62
+ test_files: []