Skip to content

Commit

Permalink
Merge pull request #368 from judofyr/array-last
Browse files Browse the repository at this point in the history
Implement Array#last properly
  • Loading branch information
alex committed Feb 15, 2013
2 parents 6d0c2f3 + a18ab1a commit 46ec03e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions fabfile/travis.py
Expand Up @@ -140,6 +140,7 @@ def run_specs(binary, prefix=""):
"core/array/delete_at_spec.rb",
"core/array/empty_spec.rb",
"core/array/frozen_spec.rb",
"core/array/last_spec.rb",
"core/array/length_spec.rb",
"core/array/plus_spec.rb",
"core/array/push_spec.rb",
Expand Down
12 changes: 11 additions & 1 deletion topaz/objects/arrayobject.py
Expand Up @@ -2,6 +2,7 @@

from rpython.rlib.listsort import TimSort

from topaz.coerce import Coerce
from topaz.module import ClassDef, check_frozen
from topaz.modules.enumerable import Enumerable
from topaz.objects.objectobject import W_Object
Expand Down Expand Up @@ -333,7 +334,16 @@ def first
""")

@classdef.method("last")
def method_last(self, space):
def method_last(self, space, w_count=None):
if w_count is not None:
count = Coerce.int(space, w_count)
if count < 0:
raise space.error(space.w_ArgumentError, "negative array size")
start = len(self.items_w) - count
if start < 0:
start = 0
return space.newarray(self.items_w[start:])

if len(self.items_w) == 0:
return space.w_nil
else:
Expand Down

0 comments on commit 46ec03e

Please sign in to comment.