mirror of
https://github.com/ruby/ruby.git
synced 2025-08-15 13:39:04 +02:00
Array#first
and Array#last
in Ruby
This commit is contained in:
parent
c9fd81b860
commit
0112a5b342
Notes:
git
2023-03-23 05:03:44 +00:00
2 changed files with 56 additions and 17 deletions
26
array.rb
26
array.rb
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue