e$B1sF#$H?=$7$^$9!#e(B
e$B@hF,$NMWAG0J30$NG[Ns$rJV$9e(B Array#tail
e$B$H$$$&%a%=%C%I$rDs0F$7$^$9!#e(B
[1, 2, 3, 4].tail #=> [2, 3, 4]
e$B8=>u$G$Oe(B ary[1…-1] e$B$,%$%G%#%*%`$K$J$C$F$$$k$H;W$$$^$9$,!"e(B
- e$B%$%s%G%C%/%9$KIi$NCM$r;H$C$F$$$F$d$d%H%j%C%-!<e(B
- (e$B;d$N<g4Q$G$Oe(B) e$B8+1I$($b$"$^$jNI$/$J$$e(B
- e$B$=$N$o$j$K$+$J$jIQ=Pe(B
e$B$J$N$G!"@lMQ$N%a%=%C%I$Ge(B ary.tail e$B$H=q$1$k$H$$$$$H;W$$$^$9!#e(B
e$B!Ve(BHaskell e$BG>$K$d$5$7$$!W$H$$$&8z2L$b$"$j$^$9e(B (?)
String#[] e$B$J$I$b4^$`$N$G@53N$JCM$G$O$J$$$G$9$,!“e(Bruby
e$B$N%3!<%ICf$@$1$G$be(B
193 e$B2U=j$[$I$”$j$^$9!#e(B
$ find . -name *.rb | xargs grep “[1..-1]” | wc -l
193
e$B$H$j$"$($:<BAu$7$F$_$^$7$?!#e(B
e$B$D$$$G$Ke(B Array#head e$B$re(B first e$B$Ne(B alias
e$B$K$7$F$$$^$9!#e(B
e$B$h$m$7$/$48!F$2<$5$$!#e(B
Index: array.c
— array.c (revision 13858)
+++ array.c (working copy)
@@ -785,6 +785,8 @@
- call-seq:
-
array.first -> obj or nil
-
array.first(n) -> an_array
-
-
array.head -> obj or nil
-
-
-
array.head(n) -> an_array
- Returns the first element, or the first +n+ elements, of the array.
- If the array is empty, the first form returns
nil
, and
the
@@ -832,6 +834,48 @@
}
}
-
+/* call-seq:
-
-
array.tail -> obj or nil
-
-
-
array.tail(n) -> sub_array
-
-
-
- Returns the array without the first element, or the first +n+
elements,
- Returns the array without the first element, or the first +n+
-
- of self. If the array is empty, the first form returns
-
-
nil
, and the second form returns an empty array.
-
-
- Equivalent to:
-
-
-
def tail(n = 1)
-
-
-
raise ArgumentError, "negative array size" if n < 0
-
-
-
self[n..-1] || (n == 1 ? nil : [])
-
-
-
end
-
-
-
-
a = [ "q", "r", "s", "t" ]
-
-
-
a.tail #=> ["r", "s", "t"]
-
-
-
a.tail(2) #=> ["s", "t"]
-
-
-
a.tail(5) #=> []
-
-
-
[].tail #=> nil
-
-
-
[].tail(2) #=> []
-
- */
+static VALUE
+rb_ary_tail(int argc, VALUE *argv, VALUE ary)
+{ - VALUE len;
- if (argc == 0) {
- if (RARRAY_LEN(ary) == 0) return Qnil;
- len = LONG2NUM(RARRAY_LEN(ary) - 1);
- return ary_shared_first(1, &len, ary, Qtrue);
- }
- else if (NUM2LONG(argv[0]) < 0) {
- rb_raise(rb_eArgError, “negative array size”);
- }
- else if (RARRAY_LEN(ary) < NUM2LONG(argv[0])) {
- return rb_ary_new3(0);
- }
- else {
- argv[0] = LONG2NUM(RARRAY_LEN(ary) - NUM2LONG(argv[0]));
- return ary_shared_first(argc, argv, ary, Qtrue);
- }
+}
/*
- call-seq:
-
array.fetch(index) -> obj
@@ -3247,7 +3291,9 @@
rb_define_method(rb_cArray, “at”, rb_ary_at, 1);
rb_define_method(rb_cArray, “fetch”, rb_ary_fetch, -1);
rb_define_method(rb_cArray, “first”, rb_ary_first, -1);
-
rb_define_alias(rb_cArray, “head”, “first”);
rb_define_method(rb_cArray, “last”, rb_ary_last, -1); -
rb_define_method(rb_cArray, “tail”, rb_ary_tail, -1);
rb_define_method(rb_cArray, “concat”, rb_ary_concat, 1);
rb_define_method(rb_cArray, “<<”, rb_ary_push, 1);
rb_define_method(rb_cArray, “push”, rb_ary_push_m, -1);
Index: test/ruby/test_array.rb
===================================================================
— test/ruby/test_array.rb (revision 13858)
+++ test/ruby/test_array.rb (working copy)
@@ -720,6 +720,18 @@
assert(a1.hash != a3.hash)
end -
def test_head
-
assert_equal(3, @cls[3, 4, 5].head)
-
assert_equal([], @cls[3, 4, 5].head(0))
-
assert_equal([3], @cls[3, 4, 5].head(1))
-
assert_equal([3, 4], @cls[3, 4, 5].head(2))
-
assert_equal([3, 4, 5], @cls[3, 4, 5].head(3))
-
assert_equal([3, 4, 5], @cls[3, 4, 5].head(4))
-
assert_raise(ArgumentError) { @cls[3, 4, 5].head(-1) }
-
assert_equal(nil, @cls[].head)
-
assert_equal([], @cls[].head(1))
-
end
-
def test_include?
a = @cls[ ‘cat’, 99, /a/, @cls[ 1, 2, 3] ]
assert(a.include?(‘cat’))
@@ -1103,6 +1115,18 @@
assert_equal(@cls[], @cls[].sort!)
end -
def test_tail
-
assert_equal([4, 5], @cls[3, 4, 5].tail)
-
assert_equal([3, 4, 5], @cls[3, 4, 5].tail(0))
-
assert_equal([4, 5], @cls[3, 4, 5].tail(1))
-
assert_equal([5], @cls[3, 4, 5].tail(2))
-
assert_equal([], @cls[3, 4, 5].tail(3))
-
assert_equal([], @cls[3, 4, 5].tail(4))
-
assert_raise(ArgumentError) { @cls[3, 4, 5].tail(-1) }
-
assert_equal(nil, @cls[].tail)
-
assert_equal([], @cls[].tail(1))
-
end
-
def test_to_a
a = @cls[ 1, 2, 3 ]
a_id = a.id