.

Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

Monday, June 4, 2012

[Ruby] String Replacer

          Hello, Reader ^^! Back again with source code. This time is String Replacer which is made using Ruby programming language. This shows how to do a string replace. Mainly, you can use gsub to replace the string. In this one, I also added save and load feature so it easier to read very long string. Okay, that's all I can say. Thank you for reading ^^!










Friday, April 27, 2012

[Ruby] Random Choice With Hash Table

          Hello, Reader ^^! Back again with source code. This time is Random Choice With Hash Table which is made using Ruby programming language. This shows how to do a random number and use hash table combined into choosing output of hash table by randomize the choice. The input of the left side of the hast table must be integer type. Other still work, but you need to hash it before randomizing hashing it. Okay, that's all I can say. Thank you for reading ^^!









Tuesday, April 24, 2012

[Ruby] [FXRuby] Read (Load) File

          Hello, Reader ^^! Back again with source code. This time is Text Example which is written in Ruby programming language and FXRuby extension. In this one, it will show load file dialogue. It will get the path of the to be loaded/read. However, it only get the file path, it will not load it immediately, so you need to define the read/load in standard ruby after using the file load dialogue. There's example how to do it in the source code. Okay, that's all I can say. Thank you for reading ^^!






Download/View Source Code
- Dropbox
- Dropbox (Repository)
- GitHub
- GitHub (Repository) 







[Ruby] [FXRuby] Write (Save) File

          Hello, Reader ^^! Back again with source code. This time is Text Example which is written in Ruby programming language and FXRuby extension. In this one, it will show save file dialogue. It will get the path of the to be saved/written. However, it only get the file path, it will not save it immediately, so you need to define the write/save in standard ruby after using the file save dialogue. There's example how to do it in the source code. Okay, that's all I can say. Thank you for reading ^^!












[Ruby] [FXRuby] User Defined Message Dialogue

          Hello, Reader ^^! Back again with source code. This time is Text Example which is written in Ruby programming language and FXRuby extension. In this one, how to pop up message dialogue using your own defined message dialogue. There is already built-in message dialogue but I prefer to define my own message dialogue so I can define my own message icon in it. Okay, that's all I can say. Thank you for reading ^^!











[Ruby] [FXRuby] Input From Text Field

          Hello, Reader ^^! Back again with source code. This time is Text Example which is written in Ruby programming language and FXRuby extension. In this example, it shows how to get input from a text field. You need to call the FXRuby component as variable to achieve it for calling the variable value. Okay, that's all I can say. Thank you for reading ^^!











[Ruby] [FXRuby] Text Example

          Hello, Reader ^^! Back again with source code. This time is Text Example which is written in Ruby programming language and FXRuby extension. In this example, it will display how to use text. You can use FXLabel to achieve it. That way, there will be text on window form. Its value can be also changed to other text. Okay, that's all I can say. Thank you for reading ^^!











Sunday, April 15, 2012

[Ruby] Procedure Syntax

          Hello, Reader ^^! Back again with source code. This time is a basic syntax Procedure in Ruby programming language. This source code is to show how to do Procedure statement in Ruby language. Here's the template:


def <ProcedureName>(<input1>,...,<inputN>)
       <Statement>
end

Note:  <input1>,...,<inputN>  is optional, only use it if there is input required in the function

Okay, that's all I can say. Thank you for reading ^^!








[Ruby] Function Syntax

          Hello, Reader ^^! Back again with source code. This time is a basic syntax Function in Ruby programming language. This source code is to show how to do  Function statement in Ruby language. Here's the template:


def <FunctionName>(<input1>,...,<inputN>)
       <Statement>
       return <value>
end

Note:  <input1>,...,<inputN>  is optional, only use it if there is input required in the function

Okay, that's all I can say. Thank you for reading ^^!








[Ruby] Case Syntax

          Hello, Reader ^^! Back again with source code. This time is a basic syntax Case in Ruby programming language. This source code is to show how to do Case statement in Ruby language. Here's the template:



case <variable name>
   when <variable condition 1> then
       <statement 1 if variable fulfills condition 1>
   when <variable condition 2> then
       <statement 2 if variable fulfills condition 2>
   when  <variable condition 3> then
       <statement 3 if variable fulfills condition 3>
   when  <variable condition N> then
       <statement N if variable fulfills condition N>
   else (optional)
       <statement if variable doesn't fulfill any condition>
end


Note: "else" is optional


Okay, that's all I can say. Thank you for reading ^^!








[Ruby] If-Then-Else Syntax

          Hello, Reader ^^! Back again with source code. This time is a basic syntax If-Then-Else in Ruby programming language. This source code is to show how to do If-Then-Else statement in Ruby language. Here's the template:


if <condition>
       <statement if true> 
else
       <statement if false>
end


Okay, that's all I can say. Thank you for reading ^^!








[Ruby] If-Then Syntax

          Hello, Reader ^^! Back again with source code. This time is a basic syntax If-Then in Ruby programming language. This source code is to show how to do If-Then statement in Ruby language. Here's the template:


if <condition>
       <statement if true> 
end


Okay, that's all I can say. Thank you for reading ^^!








Monday, March 19, 2012

[Ruby] Rail Fence Cipher Extended

          Hello, Reader ^^! Back again with source code. This time is a  Rail Fence Cipher Extended which is made from Ruby programming language. This one is a bit difficult and tricky. The formula needed is also tricky. The coding of encryption is easier than the decryption. The difference with the normal version is that the Extended version also accept non-number and non-alphabet character. The alphaber is case-sensitive in this version. Now for a bit of demonstration:


Take a string "Hello World!" 
Don't remove any character unlike the non-extended version.


Now, let's arrange them like this:


H   o   r
 e l   o l !
  l   w   d


Now, based on its arranged form, from left to right and below, it is turned into HOLELWRDLO as the encryption result. That's how the Rail Fence Cipher do.


Okay, that's all I can say. Thank you for reading ^^!







Sunday, March 18, 2012

[Ruby] CFB (Cipher Feedback)

          Hello, Reader ^^! Back again with source code. This time is is CFB (Cipher Feedback) which is made using Ruby programming language. This one, like CBC, also encrypts block per block from previous blocks. The first block uses Initialization Vector. The IV (and the next xor-ed plaintext and Encryption Block), will be encrypted into Encryption Block. After that, it will xor-ed with plaintext which will be the ciphertext. A mistake just a bit can lead mistake also in the encryption and decryption. The Encryption Block used in this example is the simple XOR. You can define your own Encryption Block like doing shift left, shift right, etc. Now, let me demonstrate a bit:

plain text (in bit) : 0101111111010101
key : 10101
IV generated 10001
Encryption Block = a xor b

Now, splitting them into 5 blocks (based on key's length): 01011|11111|01010|1
The last part length is not 5, so let's padding it with 0 bits : 10000



IV(only 1st block), then from the Fifth row 10001 10001 11011 01110 
Key01011 10101 11111 00000 
Encryption Block (a xor b; a is first row, b is key) 11010001000010001110
Plaintext (in bits)
01011 11111 01010 10000
Xor-ed (Plaintext with third row)  
10001110110111011111


The result of encryption is 10001110110111011111. If you see at the 3rd column, at the 2nd row, you will see the value is taken from 2nd column, 5th row. Yup, that's why it is chaining.

Okay, that's all I can say. Thank you for reading ^^!







Saturday, March 17, 2012

[Ruby] CBC (Cipher-Block Chaining)

          Hello, Reader ^^! Back again with source code. This time is is CBC (Cipher-Block Chaining) which is made using Ruby programming language. This one, uses encryption per block which came from the previous block. This way, the encryption becomes stronger, not only that, with Initialization Vector which is generated random, it made encryption more unique even with the same key. The IV can be kept or not, because when decryption, it won't effect the other block not like when enciphering. Now, let me demonstrate a bit:

plain text (in bit) : 0101111111010101
key : 10101
IV generated 10001

Now, splitting them into 5 blocks (based on key's length): 01011|11111|01010|1
The last part length is not 5, so let's padding it with 0 bits : 10000




Plaintext(in bits)01011111110101010000
Key per block 10101 10101 10101 10101 
IV(only 1st block), then with the ciphered block0101110101 11111 00000 
Xor-ed10101111110000000101


The result of encryption is 10101111110000000101. If you see at the 3rd column, at the 3rd row, you will see the value is taken from 2nd column, 4th row. Yup, that's why it is chaining.

Okay, that's all I can say. Thank you for reading ^^!







[Ruby] ECB (Electronic Code Book)

          Hello, Reader ^^! Back again with source code. This time is is ECB (Electronic Code Book) which is made using Ruby programming language. Let me explain it again like the C++ before. Encryption using ECB is separating plaintext bit into blocks (key). After that, each block each encrypted with the block key. When the plaintext's length is not divideable by the key's length, pad it with all 0s or all 1s or 101010... Now, let's me demonstrate a bit:

plain text (in bit) : 0101111111010101
key : 01101

Now, splitting them into 5 blocks (based on key's length): 01011|11111|01010|1
The last part length is not 5, so let's padding it with 0 bits : 10000






Plaintext(in bits)01011111110101010000
Key per block01011010110101101011
Xor-ed00000101000000111011


The result of encryption is 00000101000000111011

Okay, that's all I can say. Thank you for reading ^^!







Friday, March 16, 2012

[Ruby] Rail Fence Cipher

          Hello, Reader ^^! Back again with source code. This time is a  Rail Fence Cipher which is made from Ruby programming language. This one is a bit difficult and tricky. The formula needed is also tricky. The coding of encryption is easier than the decryption. Now for a bit of demonstration:


Take a string "Hello World!"
Remove all non-number and non-alphabet. Uppercase all of the string. Remove all the space. The result:
"HELLOWORLD"


Now, let's arrange them like this:


H   O   L
 E L W R D
  L   O


Now, based on its arranged form, from left to right and below, it is turned into HOLELWRDLO as the encryption result. That's how the Rail Fence Cipher do.


Okay, that's all I can say. Thank you for reading ^^!







Tuesday, March 13, 2012

[Ruby] ROT13

          Hello, Reader ^^! Back again with source code. This time is a simple ROT13. This one is a bit tricky but still easy. Playing with ASCII index is needed. To check whether a character is an alphabet, simple use the regular expression /[A-Z]/ for uppercase and /[a-z]/ for regular case. After that, switch the index by 13, then you will get the ROT13 result. Okay, that's all I can say. Thank you for reading ^^!








[Ruby] Palindrome Check

          Hello, Reader ^^! Back again with source code. This time is a simple Palindrome Check. The only needed is using the built-in reverse function. Then compare it with the original string. If equal, then it is palindrome. Example, a string "pizza azzip" is a palindrome. One more thing, in this example, it is case-sensitive. Okay, that's all I can say. Thank you for reading ^^!







Sunday, March 11, 2012

[Ruby] Factorial

          Hello, Reader ^^! Back again with source code. This time is a simple Factorial. Yup, it is just doing a factorial operation :p. This one shows how to create a function and raise (Ruby exception version). It handles whether the input is lower than 0. The operation is also using recursive method. Okay, that's all I can say. Thank you for reading ^^!