Ruby Integer digits function with example

The digits function in Ruby returns the digits place of converting the number to the given base in the parameter. If there is no such parameter is provided, then the default base is taken as 10.

Syntax: number.digits(base)

Parameter: The function takes the integer whose conversion is to be done. It takes a non-mandatory parameter base to which the conversion is to be done. The minimum value in the base can be 2.

Return Value: The function returns the digits place in new line after conversion.

Example #1:



filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby program of Integer digits function
  
# Initializing the numbers 
num1 = 6788
num2 = 89
   
# Prints the number 
# after base conversion 
puts num1.digits
puts
puts num2.digits(3)

chevron_right


Output:

8
8
7
6

2
2
0
0
1

Example #2:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby Program of Integer digits function
# Initializing the numbers
num1 = 563 num2 = 12
   
# Prints the number
# after base conversion
puts num1.digits
puts
puts num2.digits(6)

chevron_right


Output:

3
6
5

0
2
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.