Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Integer#to_d #649

Merged
merged 3 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions vm/integer.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,21 @@ func builtinIntegerInstanceMethods() []*BuiltinMethodObject {
}
},
},
// Returns the `Decimal` conversion of self.
//
// ```Ruby
// 100.to_d # => '100'.to_d
// ```
// @return [Decimal]
{
Name: "to_d",
Fn: func(receiver Object, sourceLine int) builtinMethodBody {
return func(t *thread, args []Object, blockFrame *normalCallFrame) Object {
r := receiver.(*IntegerObject)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add argument check here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do that.

I also found that arg checks are missing in other methods so I'd add them in another PR.

return t.vm.initDecimalObject(intToDecimal(r))
}
},
},
// Returns the `Float` conversion of self.
//
// ```Ruby
Expand Down
9 changes: 9 additions & 0 deletions vm/integer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,17 @@ func TestIntegerConversion(t *testing.T) {
expected interface{}
}{
{`100.to_i`, 100},
{`-100.to_i`, -100},
{`100.to_f`, 100.0},
{`-100.to_f`, -100.0},
{`100.to_s`, "100"},
{`100.to_d.to_i`, 100},
{`-100.to_d.to_i`, -100},
{`100.to_d.numerator.to_i`, 100},
{`100.to_d.denominator.to_i`, 1},
{`-100.to_d.numerator.to_i`, -100},
{`-100.to_d.denominator.to_i`, 1},
{`100.to_d.class.name`, "Decimal"},
}

for i, tt := range tests {
Expand Down