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:
num1 = 6788
num2 = 89
puts num1.digits
puts
puts num2.digits( 3 )
|
Output:
8
8
7
6
2
2
0
0
1
Example #2:
num1 = 563 num2 = 12
puts num1.digits
puts
puts num2.digits( 6 )
|
Output:
3
6
5
0
2