Hackerrank Ruby - Strings - Encoding Solution
2 min read

Hackerrank Ruby - Strings - Encoding Solution

Hackerrank Ruby - Strings - Encoding Solution

In Ruby, strings are objects of the String class, which defines a powerful set of operations and methods for manipulating text (e.g., indexing, searching, modifying, etc.). Here are a few easy ways to create Strings:my_string = "Hello." # create a string from a literalmy_empty_string = String.new # create an empty stringmy_copied_string = String.new(my_string) # copy a string to a new variable

Until Ruby , Strings were nothing but a collection of bytes. Data was indexed by byte count, size was in terms of number of bytes, and so on. Since Ruby , Strings have additional encoding information attached to the bytes which provides information on how to interpret them. For example, this code:str = "With ♥!"print("My String's encoding: ", str.encoding.name) print("\nMy String's size: ", str.size)print("\nMy String's bytesize: ", str.bytesize)

produces this output:

My String's encoding: UTF-8
My String's size: 7
My String's bytesize: 9

You can make the following observations about the above code:

  • The string literal creates an object which has several accessible methods.
  • The string has attached encoding information indicating it's an UTF-8 string.
  • A String's size corresponds to the umber of characters we see.
  • A String's bytesize corresponds to the actual space taken by the characters in memory (the ♥ symbol requires  bytes instead of ).

Although  is the most popular (and recommended) encoding style for content, Ruby supports  other encodings (try  for the full list). With this in mind, we should learn how to convert between different encodings.

In this challenge, we practice setting the encoding information for some string of text using Ruby's Encoding methods. Write a function named transcode which takes a  encoded string as a parameter, converts it to an  encoded string, and returns the result.

Input Format.

Our hidden code checker will call your function, passing it an  encoded string as an argument.


  • Your function must be named transcode.

Output Format.

Your function must return an  encoded string.

Solution in ruby

Approach 1.

def transcode(s)
    return s

Approach 2.

def transcode(arg)
    return arg

Approach 3.

# Enter your code here. 
def transcode(iso)

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.