Table of contents
1.
Introduction 
2.
ASCII
2.1.
Encoding in ASCII 
3.
UNICODE
3.1.
Example
3.2.
Output
4.
String Encoding
4.1.
For Example:-
4.2.
Output
5.
Character Encoding
6.
Ruby Encoding Method
7.
String#force Encoding: 
7.1.
Program
7.2.
String#encode:
7.3.
Program
8.
Frequently Asked Question
8.1.
What are strings in Ruby?
8.2.
In Ruby, what is the purpose of the global variable $?
8.3.
What does it mean by freezing string in Ruby?
8.4.
What are iterators in Ruby?
9.
Conclusion
Last Updated: Mar 27, 2024

ASCII and BINARY Encodings in Ruby

Author Ayush Mishra
0 upvote

Introduction 

We see information in text form everywhere in today's software and technology. Text that isn't presented to us in a media format, such as photos or videos, is kept as numbers in the computer, with encoding serving as the mechanism by which we understand how they should be read correctly. This is especially true when transferring text media across languages when a person's system may not accept it. As a result, as developers, we should get familiar with the tools we have for encoding and how we may best accomplish our goals.

This article will teach you how character encodings operate and how Ruby implements them.

ASCII

Around 1963, a computer's standard for encoding and decoding characters was developed based on knowledge of  Morse code and very early computers. Because it only included 127 letters initially, the English alphabet plus additional symbols, this system was rather straightforward.

ASCII stands for “American Standard Code for Information Interchange.” Each character in ASCII was assigned a decimal number that could be converted into binary information.

Encoding in ASCII 

Encoding in ASCII includes:

  • Characters (a-z, A-Z)
  • Symbols
  • Numbers (0-9)
  • Control characters

 

Consider the following scenario:

Because "B" is 66 in ASCII, we must convert 66 to binary code.

If you're not sure how it works, here's a simple explanation: We begin by dividing 66 by 2 and keep going till we arrive to 0. We add 1 as a remnant if the division isn't exact:

63 / 2 = 33 + 0
33 / 2 = 16 + 1
16 / 2 = 8 + 0
8 / 2  = 4 + 0
4 / 2  = 2 + 0
2 / 2  = 1 + 0
1 / 2  = 0 + 1
After that, we reverse the order of the remainders:
1000010

UNICODE

Unicode is a character encoding scheme that can store up to a million distinct characters in a single string.

ASCII can only encode up to 127 different characters (256 with extended ASCII); this limits the number of characters we can represent is limited.

Below given an example of an invisible Unicode function:-

Example

def ​
  puts "Invisible function"
end

Output

Invisible function

String Encoding

Strings in Ruby are made up of an array of bytes and an encoding object. The encoding object on the string may be accessed by using encoding on the string object.

For Example:-

puts "coding".encoding
puts "Ninjas\n\n".encoding

Output

UTF-8
UTF-8

Character Encoding

Strings are used in every computer language. You use them as input sometimes and display them as output other times. On the other hand, your computer does not comprehend the concept of "strings." It just recognizes 1s and 0s as bits.The process of converting strings to bits is called Character Encoding.

Ruby Encoding Method

There are two main methods of string encoding:

  1.  String#force_encoding 
  2.  String#encode

String#force Encoding: 

The force encoding function will only modify whatever encoding object the string points to, not the string itself. It just modifies the encoding object associated with the string, not the bytes of the string.

It means that we know the bits for the characters are right, and all we need to do now is declare how those bits should be translated into characters.

After using the force encode function, we can observe that the encoding return value changes:

Program

>> x = 'Coding Ninjas'
>> x.encoding
=> #<Encoding:UTF-8>
>> x.force_encoding "US-ASCII"
=> "Coding Ninjas"
>> x.encoding
=> #<Encoding:US-ASCII>

String#encode:

The String#encode method will generate a new string from the bytes of the old one and attach the encoding object with it.

It will convert the bits that make up the characters from whatever encoding the string is to our desired encoding.

We can see that x's encoding is unchanged, and executing encode gives a new stringy that corresponds to the new encoding:

Program

>> x = 'Coding Ninjas'
>> x.encoding
=> #<Encoding:UTF-8>
>> y = x.encode("US-ASCII")
>> x.encoding
=> #<Encoding:UTF-8>
>> y.encoding
=> #<Encoding:US-ASCII>

Frequently Asked Question

What are strings in Ruby?

An arbitrary series of bytes, often representing characters, is held and manipulated by the Ruby string object. String::new or literals are used to generate them.

In Ruby, what is the purpose of the global variable $?

Because the global variable is defined in Ruby, it may be accessed from anywhere in the program because it has full scope. In Ruby, $ prepend is used to use global variables.

What does it mean by freezing string in Ruby?

Strings are immutable in most computer languages. It indicates that you can't change an existing string; you can only make a new one out of it.

Strings are not immutable by default in Ruby. The freezing technique can be used to make them immutable.

What are iterators in Ruby?

In object-oriented programming, an iterator is a notion. Iteration refers to repeating a task multiple times, as in a loop.

Conclusion

In this blog, using encoding tables, we learned how computers generate characters from integers!. You've also known about ASCII and Unicode.

However, this isn't enough because there's always more to learn and appreciate about Ruby.

After reading about binary encoding in Ruby, are you willing to read/explore more articles on the subject of Ruby? Don't worry; Coding Ninjas will handle everything for you. See Ruby on Rails: An Introduction, Ruby Directory StructureRuby on Rails, and Ruby versus Python.

 

Recommended problems -

 

Close your eyes and visit Coding Ninjas Studio to practice top problems, take practice exams, and read interview stories, among other things.

Happy Learning!

Live masterclass