Learn and Learn

Learn And Learn - great place for tutorials, references and how-to

How to use Base64 Class In Ruby

Base64 class is used to encode and decode data into a readable text or characters, which can be used or printed later on. To use Base64 in Ruby you can use base64.encode64().

Base64 () Method in Ruby

base64() method exists in Standard base64 Library of Ruby. The purpose of this method is to encode and decode data into a readable text or characters.

 

Syntax of base64() in Ruby

The syntax of base64() method in Ruby is:

 

Parameter of base64() in Ruby

The parameter x must be any valid string that require to be encoded or decoded.Note: If the x parameter/argument is, empty or integer, base64 method will return an error.

 

Return Value of Base64() in Ruby

Base64() method will return secret code for the more than one given strings.

 

How to Encode Data with base64() Method in Ruby?


Output:

VGVuIE1pbGxpb24h

In the above output of Ruby code, you can see the secret code for the given string ‘I am going to UK’.

 

How to Encode more than one Data Strings with base64() Method in Ruby?

To encode more than one Data Strings please see the example below:

Output:

SSBhbSBnb2luZyB0byBVSyE=
SSB3aWxsIGNvbWUgYmFjayBuZXh0IG1vbnRoIQ==

Now in the output of above code, you got the secret code for more than one strings in a single code.
This is how base64 method encodes data. Let see how to decode these secret codes in Ruby.

 

Decode Data with base64() Method in Ruby


Output:

I am going to UK!

NOTE: This is the method that how to decode an already encoded data in Ruby. Now let see how to decode more than one secret codes in Ruby.

 

Decode more than one Data Strings with base64() Method in Ruby


Output:

I am going to UK!
I will come back next month!

LearnAndLearn.com © 2018 - All rights reserved