From: Arlen Cuss Date: 2008-04-30T09:23:10+09:00 Subject: Re: Array#first is not Array#[0] ------=_Part_8416_28996032.1209514983292 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Content-Disposition: inline Hi, On Wed, Apr 30, 2008 at 4:50 AM, Brian Adkins wrote: > "David A. Black" writes: > > The real question is: why should it? They're completely separate > > methods. > > I can't think of a good reason to ever have: Array#first != Array#[](0) > > They're still separate methods. The point is, internally, Ruby saves another dynamic method dispatch by hard-coding Array#first to just grab the first item from the internal data structure -- just like calling Array#[](0) would, but it's faster. It might even make a noticeable difference in some application that calls Array#first and #last a lot.. (I don't propose such cases do exist, but every small part counts) array.c: static VALUE rb_ary_first(int argc, VALUE *argv, VALUE ary) { if (argc == 0) { if (RARRAY_LEN(ary) == 0) return Qnil; return RARRAY_PTR(ary)[0]; } else { return ary_shared_first(argc, argv, ary, Qfalse); } } Here seen retrieving the first item manually. Arlen ------=_Part_8416_28996032.1209514983292--