Open In App

Ruby Integer digits function with example

Last Updated : 07 Jan, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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:




# 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)


Output:

8
8
7
6

2
2
0
0
1

Example #2:




# 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)


Output:

3
6
5

0
2

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads