Ruby Integer chr function with example

The chr function in Ruby returns the string containing the character represented by the int’s value according to encoding.

Syntax: string.chr([encoding])

Parameter: The function takes the integer value whose encoding is to be done. It takes a non-mandatory parameter encoding if encoding is to be done according to that.



Return Value: The function returns character.

Example #1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby Program of Integer chr function
  
# Initializing the numbers 
num1 = 65
num2 = 66
num3 = 97
num4 = 245
   
# Prints the chr 
# after encoding 
puts num1.chr
puts num2.chr
puts num3.chr
puts num4.chr(Encoding::UTF_8)

chevron_right


Output:

A
B
a
õ

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby Program of Integer chr function
  
# Initializing the numbers 
num1 = 119
num2 = 68
num3 = 89
num4 = 255
   
# Prints the chr 
# after encoding 
puts num1.chr
puts num2.chr(Encoding::UTF_8)
puts num3.chr
puts num4.chr(Encoding::UTF_8)

chevron_right


Output:

w
D
Y
ÿ


My Personal Notes arrow_drop_up

The function of education is to teach one to think intensively and to think critically

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.