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:
- String#force_encoding
- 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 Structure, Ruby 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!