From c3e9839e876389db74f005a3c4ecb9bca96d810e Mon Sep 17 00:00:00 2001 From: Magnus Holm Date: Thu, 14 Feb 2013 21:01:45 +0100 Subject: [PATCH 1/2] Implement Array#last properly --- fabfile/travis.py | 1 + topaz/objects/arrayobject.py | 12 ++++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/fabfile/travis.py b/fabfile/travis.py index 2c6f8dc88..ac6256534 100644 --- a/fabfile/travis.py +++ b/fabfile/travis.py @@ -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", diff --git a/topaz/objects/arrayobject.py b/topaz/objects/arrayobject.py index 99a520fd6..4049b942f 100644 --- a/topaz/objects/arrayobject.py +++ b/topaz/objects/arrayobject.py @@ -332,8 +332,16 @@ def first end """) - @classdef.method("last") - def method_last(self, space): + @classdef.method("last", count="int") + def method_last(self, space, count=None): + if count is not None: + 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: From a18ab1aa5a7aed48038ca4f44848452dfdc9a4f2 Mon Sep 17 00:00:00 2001 From: Magnus Holm Date: Thu, 14 Feb 2013 21:40:07 +0100 Subject: [PATCH 2/2] Make Array#last translatable --- topaz/objects/arrayobject.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/topaz/objects/arrayobject.py b/topaz/objects/arrayobject.py index 4049b942f..0b200a009 100644 --- a/topaz/objects/arrayobject.py +++ b/topaz/objects/arrayobject.py @@ -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 @@ -332,9 +333,10 @@ def first end """) - @classdef.method("last", count="int") - def method_last(self, space, count=None): - if count is not None: + @classdef.method("last") + 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