Skip to content

Commit

Permalink
Implement Object#tap
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 committed Oct 6, 2019
1 parent ae5e7c3 commit 83aa84f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions vm/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,18 @@ var builtinClassCommonInstanceMethods = []*BuiltinMethodObject{

},
},
{
Name: "tap",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
if blockFrame == nil {
return t.vm.InitErrorObject(errors.InternalError, sourceLine, errors.CantYieldWithoutBlockFormat)
}

t.builtinMethodYield(blockFrame, receiver)

return receiver
},
},
{
Name: "thread",
Fn: func(receiver Object, sourceLine int, t *Thread, args []Object, blockFrame *normalCallFrame) Object {
Expand Down
33 changes: 33 additions & 0 deletions vm/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,36 @@ func TestObjectClassSuperclass(t *testing.T) {
v.checkSP(t, i, 1)
}
}

func TestObjectTapMethod(t *testing.T) {
tests := []struct {
input string
expected interface{}
}{
{
`
a = 1
a.tap do |int|
int + 1
end
`, 1},
{
`
a = 1
b = 2
a.tap do |int|
b = int + b
end
b
`, 3},
}

for i, tt := range tests {
v := initTestVM()
evaluated := v.testEval(t, tt.input, getFilename())
VerifyExpected(t, i, evaluated, tt.expected)
v.checkCFP(t, i, 0)
v.checkSP(t, i, 1)
}
}

0 comments on commit 83aa84f

Please sign in to comment.