Array#first and Array#last in Ruby

This commit is contained in:
Koichi Sasada 2023-03-10 01:33:00 +09:00
parent c9fd81b860
commit 0112a5b342
Notes: git 2023-03-23 05:03:44 +00:00
2 changed files with 56 additions and 17 deletions

View file

@ -66,4 +66,30 @@ class Array
Primitive.ary_sample(random, n, ary)
end
end
def first n = unspecified = true
if Primitive.mandatory_only?
Primitive.attr! :leaf
Primitive.cexpr! %q{ ary_first(self) }
else
if unspecified
Primitive.cexpr! %q{ ary_first(self) }
else
Primitive.cexpr! %q{ ary_take_first_or_last_n(self, NUM2LONG(n), ARY_TAKE_FIRST) }
end
end
end
def last n = unspecified = true
if Primitive.mandatory_only?
Primitive.attr! :leaf
Primitive.cexpr! %q{ ary_last(self) }
else
if unspecified
Primitive.cexpr! %q{ ary_last(self) }
else
Primitive.cexpr! %q{ ary_take_first_or_last_n(self, NUM2LONG(n), ARY_TAKE_LAST) }
end
end
end
end