"0" Ruby on Rails series review
[Ruby on Rails Series] 1, Development environment preparation: Installation of VMware and Linux
[Ruby on Rails series] 2, development environment preparation: Ruby on Rails development environment configuration
[Ruby on Rails series]3, initial rails: Developing the first Web program using Rails
[Ruby on Rails series]4, featured: Internationalization of rails applications [i18n]
[Ruby on Rails series]5, topics: Talk about SaSS
"1" Task target
The main is to implement a simple code generator, and published to the Web, the completion of the following example: http://ronakey.herokuapp.com/
Expected to be divided into the next 3 completed:
(1) [Ruby on Rails series]6, a simple code-word generator and interpreter (above)
The previous article is mainly about the introduction of Ruby language grammar, and the main algorithm implementation of code generator
(2) [Ruby on Rails series]7, a simple code-word generator and interpreter (middle)
Medium is mainly based on the Ruby on Rails web development, the code-word generator algorithm embedded in the Web program
(3) [Ruby on Rails series]8, a simple code-word generator and interpreter (below)
The next article focuses on how to deploy the developed ROR program to the Internet using the Heroku platform
"2" code generator
The main algorithm: using mobile phone nine lattice keyboard and Qwer keyboard mapping relationship. The mapping table is as follows:
Mapping table:
Nine Gongge keyboard |
Letters |
Qwer Letter |
21st |
A |
Q |
22 |
B |
W |
23 |
C |
E |
31 |
D |
R |
32 |
E |
T |
33 |
F |
Y |
41 |
G |
U |
42 |
H |
I |
... |
... |
... |
Example:
Input code: 4274439123334341
Decryption via the above cipher list is available:iloveyou
"3" Algorithm implementation
First, we need to establish a mapping table for encryption and decryption, the data structure of the above map table is Key-value, in Ruby, using the hash class to achieve the above data structure:
@numtocodetable =Hash. []( " +"="Q"," A"="W"," at"="e"," to"="R"," +"="T"," -"="y", " A"="u"," the"="I"," +"="o","Wuyi"="P"," the"="a"," -"="s", " A"="D"," +"="F"," the"="g"," in"="h"," the"="J"," the"="k", " About"="L","Bayi"="Z"," the"="x"," the"="C"," the"="v"," the"="b", " the"="N","94"="m") @codetonumtable=Hash. []( "Q"=" +","W"=" A","e"=" at","R"=" to","T"=" +","y"=" -", "u"=" A","I"=" the","o"=" +","P"="Wuyi","a"=" the","s"=" -", "D"=" A","F"=" +","g"=" the","h"=" in","J"=" the","k"=" the", "L"=" About","Z"="Bayi","x"=" the","C"=" the","v"=" the","b"=" the", "N"=" the","m"="94")
It declares 2 hash-type variables, @numtocodetable implementation of a numeric-to-letter mapping table, @codetonumtable a mapping table that implements letters to numbers.
>>
Second, the implementation of code generation: User input plaintext (letters), by converting into ciphertext (number):
def self.codetonum (code) "" = Code codeinput.split (//). Each do |item| = num + @codetonumtable [item] end return numend
The rationale is that the string is first divided into arrays, for each letter in the string, mapped according to the above mapping table, the conversion of letters to numbers, and eventually all the letters in the string into numbers, and return.
In Ruby, the string is segmented using the [String].split () method, as shown above codeinput.split (//) implements a character array that divides the string codeinput into one, where (//) represents the split pattern, with regular expression syntax After splitting the array, the [Array].each iterator supports the traversal of the array (similar to the Foreach Loop, the map in the function language), and uses the Hashtable to convert the letters to numbers in each iterator.
>>
Finally, is the code of the Interpreter: User input ciphertext (number), by conversion to get Clear (letter):
def self.numtocode (num) "" = num = Input.scan (/[0-9]{2}/) |item| = p + @numtocodetable [item] end return pend
The rationale is similar to the generator, which is not explained in detail here.
It is important to note that the 22 segmentation of the input number is required in this algorithm, the method used is the [String].scan () method instead of the split method, and the scan method is to process the string according to the syntax of the argument's regular expression and save the array Unlike split: The Split method splits a string according to a regular expression, and scan browses the string based on a regular expression. The regular expression (/[0-9]{2}/) represents the number {2} bit of [0-9], at which point the scan method can be used to divide the string 22 exactly.
"4" Implementation effect
http://ronakey.herokuapp.com/
(1) Enter plaintext ILOVEYOU:
(2) Input 4274439123334341:
In the next section, I'll cover the use of this algorithm for web framework Ruby on Rails to see how to embed a code generator algorithm into a Web page.
[Ruby on Rails series]6, a simple code-word generator and interpreter (above)