Skip to content

Commit f51f789

Browse files
committed
[GH #1612]: Fix missing String#to_d definition in BigDecimal.
PullRequest: truffleruby/686
2 parents 9fea753 + 96e0aa4 commit f51f789

File tree

7 files changed

+53
-7
lines changed

7 files changed

+53
-7
lines changed

lib/mri/bigdecimal/util.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ class String
6666
#
6767
# See also BigDecimal::new.
6868
#
69+
# TruffleRuby: MRI defines this method in C. We define it in Ruby for simplicity & clarity.
70+
def to_d
71+
begin
72+
BigDecimal(self)
73+
rescue ArgumentError
74+
BigDecimal(0)
75+
end
76+
end
6977
end
7078

7179

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
require_relative '../../spec_helper'
2+
require 'bigdecimal'
3+
require 'bigdecimal/util'
4+
5+
describe "BigDecimal's util method definitions" do
6+
7+
describe "#to_d" do
8+
9+
it "should define #to_d on Integer" do
10+
42.to_d.should == BigDecimal(42)
11+
end
12+
13+
it "should define #to_d on Float" do
14+
0.5.to_d.should == BigDecimal(0.5, Float::DIG)
15+
1.234.to_d(2).should == BigDecimal(1.234, 2)
16+
end
17+
18+
it "should define #to_d on String" do
19+
"0.5".to_d.should == BigDecimal(0.5, Float::DIG)
20+
"45.67 degrees".to_d.should == BigDecimal(45.67, Float::DIG)
21+
end
22+
23+
it "should define #to_d on BigDecimal" do
24+
bd = BigDecimal("3.14")
25+
bd.to_d.should equal(bd)
26+
end
27+
28+
it "should define #to_d on Rational" do
29+
Rational(22, 7).to_d(3).should == BigDecimal(3.14, 3)
30+
end
31+
32+
it "should define #to_d on nil" do
33+
nil.to_d.should == BigDecimal(0)
34+
end
35+
36+
end
37+
38+
describe "#to_digits" do
39+
40+
it "should define #to_digits on BigDecimal" do
41+
BigDecimal("3.14").to_digits.should == "3.14"
42+
end
43+
44+
end
45+
end
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
slow:BigDecimal is not defined unless it is required
22
fails:Kernel#BigDecimal raises ArgumentError for invalid strings
3-
fails:BigDecimal is not defined unless it is required
43
fails:Kernel#BigDecimal does not ignores trailing garbage
54
fails:Kernel#BigDecimal process underscores as Float()

spec/tags/library/bigdecimal/add_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

spec/tags/library/bigdecimal/divmod_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/tags/library/bigdecimal/modulo_tags.txt

Lines changed: 0 additions & 2 deletions
This file was deleted.

spec/tags/library/bigdecimal/remainder_tags.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)