File tree Expand file tree Collapse file tree 4 files changed +21
-4
lines changed
src/main/ruby/truffleruby/core Expand file tree Collapse file tree 4 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -39,6 +39,7 @@ Compatibility:
39
39
* Switched to the io-console C extension from C ruby for better performance and compatibility in ` irb ` .
40
40
* Coerce the message to a ` String ` for ` BasicSocket#send ` (#2209 , @HoneyryderChuck ).
41
41
* Support buffer argument for ` UDPSocket#recvfrom_nonblock ` (#2209 , @HoneyryderChuck ).
42
+ * Fixed ` Integer#digits ` implementation to handle more bases (#2224 , #2225 ).
42
43
43
44
Performance:
44
45
Original file line number Diff line number Diff line change 29
29
it "raises Math::DomainError when calling digits on a negative number" do
30
30
-> { -12345 . digits ( 7 ) } . should raise_error ( Math ::DomainError )
31
31
end
32
+
33
+ it "returns integer values > 9 when base is above 10" do
34
+ 1234 . digits ( 16 ) . should == [ 2 , 13 , 4 ]
35
+ end
36
+
37
+ it "can be used with base > 37" do
38
+ 1234 . digits ( 100 ) . should == [ 34 , 12 ]
39
+ 980099 . digits ( 100 ) . should == [ 99 , 0 , 98 ]
40
+ end
32
41
end
Original file line number Diff line number Diff line change @@ -314,8 +314,17 @@ def pred
314
314
def digits ( base = 10 )
315
315
raise Math ::DomainError , 'out of domain' if negative?
316
316
base = Primitive . rb_to_int ( base )
317
-
318
- to_s ( base ) . chars . map ( &:to_i ) . reverse
317
+ raise ArgumentError , 'negative radix' if base < 0
318
+ raise ArgumentError , "invalid radix #{ base } " if base < 2
319
+ return [ 0 ] if self == 0
320
+
321
+ result = [ ]
322
+ x = self
323
+ while x > 0
324
+ result << x % base
325
+ x /= base
326
+ end
327
+ result
319
328
end
320
329
321
330
def self . sqrt ( n )
Original file line number Diff line number Diff line change 1
1
exclude :test_Integer , "needs investigation"
2
- exclude :test_digits_for_non_integral_base_numbers , "needs investigation"
3
2
exclude :test_round , "needs investigation"
4
3
exclude :test_square_root , "needs investigation"
5
4
exclude :test_Integer_with_exception_keyword , "needs investigation"
6
5
exclude :test_bitwise_and_with_integer_coercion , "needs investigation"
7
6
exclude :test_bitwise_or_with_integer_coercion , "needs investigation"
8
7
exclude :test_bitwise_xor_with_integer_coercion , "needs investigation"
9
8
exclude :test_ceil , "needs investigation"
10
- exclude :test_digits , "needs investigation"
11
9
exclude :test_floor , "needs investigation"
12
10
exclude :test_pow , "needs investigation"
13
11
exclude :test_truncate , "needs investigation"
You can’t perform that action at this time.
0 commit comments