# Array#first is not Array#\[0\]

**URL:** <https://rubytalk.org/t/array-first-is-not-array-0/46177>\
**Category:** ruby-talk\
**Created:** [29 April 2008 07:55 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177 "2008-04-29T07:55:02Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fredrik](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fredrik](https://rubytalk.org/u/Fredrik)\
**Post date:** [29 April 2008 07:55 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/1 "2008-04-29T07:55:02Z")

</div>

I made a class that is a subclass of Array :

class MyClass \< Array  
&nbsp;&nbsp;def (i)  
&nbsp;&nbsp;&nbsp;&nbsp;"Here you go: #{super(i)}"  
&nbsp;&nbsp;end  
end

and I thought that Array#first and Array#last was just syntactic sugar  
that would translate to Array#[0] and Array#[-1] respectively. But  
it's not:

> g = MyClass.new([1,2,3])

=\> [1, 2, 3]

> g[0]

=\> "Here you go: 1"

> g.first

=\> 1

Now, isn't that really, really crazy? Am I supposed to define  
MyClass#first and MyClass#last separately???

Best regards,  
Fredrik

---

<div class="post-metadata">

**Author:** ![Phil](https://avatars.discourse-cdn.com/v4/letter/p/e95f7d/32.png) [@Phil](https://rubytalk.org/u/Phil)\
**Post date:** [29 April 2008 08:06 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/2 "2008-04-29T08:06:55Z")

</div>

Fredrik wrote:

> I made a class that is a subclass of Array :
> 
> class MyClass \< Array  
> &nbsp;&nbsp;def (i)  
> &nbsp;&nbsp;&nbsp;&nbsp;"Here you go: #{super(i)}"  
> &nbsp;&nbsp;end  
> end
> 
> and I thought that Array#first and Array#last was just syntactic sugar  
> that would translate to Array#[0] and Array#[-1] respectively. But  
> it's not:
> 
> \> g = MyClass.new([1,2,3])  
> =\> [1, 2, 3]  
> \> g[0]  
> =\> "Here you go: 1"  
> \> g.first  
> =\> 1
> 
> Now, isn't that really, really crazy? Am I supposed to define  
> MyClass#first and MyClass#last separately???

C:\scripts\>ruby temp.rb  
Here you go: 1  
1

C:\scripts\>cat temp.rb  
class MyClass \< Array  
~ def (i)  
~ "Here you go: #{super(i)}"  
~ end  
end

g = MyClass.new([1,2,3])

puts g[0]  
puts g.first

C:\scripts\>ruby -v  
ruby 1.8.6 (2008-03-03 patchlevel 114) [i386-mswin32]

Hm....

- --  
Phillip Gawlowski  
Twitter: twitter.com/cynicalryan  
Blog: [http://justarubyist.blogspot.com](http://justarubyist.blogspot.com)

~ Calvin: Can you make a living playing silly games? His Dad:  
Actually, you can be among the most overpaid people on the planet.

---

<div class="post-metadata">

**Author:** ![Fredrik](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fredrik](https://rubytalk.org/u/Fredrik)\
**Post date:** [29 April 2008 08:15 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/3 "2008-04-29T08:15:12Z")

</div>

I see that Array#each wont do what I want it to do either. I guess I  
have to do it like this:

class MyClass \< Array  
&nbsp;&nbsp;def initialize(arg)  
&nbsp;&nbsp;&nbsp;&nbsp;super(arg.map{ |i| "Here you go: #{i}"})  
&nbsp;&nbsp;end  
end

This would give me all the behavior I want, except that it's a  
wasteful use of memory if I have a really big array and want to  
include the same text in all my array elements. I just want to have  
some fixed text attached to ANY operations I do with array elements.  
To achieve that, do I really need to do it like this?

/Fredrik

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [29 April 2008 18:50 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/4 "2008-04-29T18:50:12Z")

</div>

"David A. Black" \<dblack@rubypal.com\> writes:

> **···**
>
> > On Tue, 29 Apr 2008, Fredrik wrote:
> > 
> > > What I am still thinking about though, is this:  
> > > Why on earth is Array#first not calling Array#[0] ???
> > 
> > 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)

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 April 2008 08:36 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/5 "2008-04-29T08:36:00Z")

</div>

# include the same text in all my array elements. I just want to have  
# some fixed text attached to ANY operations I do with array elements.

there are other better ways, but i just extended your initial thought/code,

class MyArray \< Array  
&nbsp;&nbsp;&nbsp;Array.methods.each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(m) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Here you go: #{super}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end  
#=\>...

g=MyArray.new [1,2,3]  
#=\> Here you go: [1, 2, 3]  
g[1]  
#=\> "Here you go: 2"  
g[0]  
#=\> "Here you go: 1"  
g.first  
#=\> 1  
g.last  
#=\> 3  
g[-1]  
#=\> "Here you go: 3"

kind regards -botp

> **···**
>
> from: Fredrik [[mailto:fredjoha@gmail.com](mailto:fredjoha@gmail.com)]

---

<div class="post-metadata">

**Author:** ![Brian\_Adkins](https://avatars.discourse-cdn.com/v4/letter/b/3ab097/32.png) [@Brian\_Adkins](https://rubytalk.org/u/Brian_Adkins)\
**Post date:** [29 April 2008 18:55 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/6 "2008-04-29T18:55:03Z")

</div>

Brian Adkins \<lojicdotcom@gmail.com\> writes:

> "David A. Black" \<dblack@rubypal.com\> writes:
> 
> > > What I am still thinking about though, is this:  
> > > Why on earth is Array#first not calling Array#[0] ???
> > 
> > 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)

Sorry about that. I'm having a bit of trouble with the Gnus news reader,  
so this got attached to the wrong message - it should've followed  
David's of course.

> **···**
>
> > > On Tue, 29 Apr 2008, Fredrik wrote:

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [29 April 2008 18:59 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/7 "2008-04-29T18:59:48Z")

</div>

Hi --

> **···**
>
> On Wed, 30 Apr 2008, Brian Adkins wrote:
> 
> > "David A. Black" \<dblack@rubypal.com\> writes:
> > 
> > > On Tue, 29 Apr 2008, Fredrik wrote:
> > > 
> > > > What I am still thinking about though, is this:  
> > > > Why on earth is Array#first not calling Array#[0] ???
> > > 
> > > 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)
> 
> That's not the question, though; the question is why #first doesn't  
> actually call #.
> 
> David
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Arlen\_Christian\_Mar1](https://avatars.discourse-cdn.com/v4/letter/a/9de053/32.png) [@Arlen\_Christian\_Mar1](https://rubytalk.org/u/Arlen_Christian_Mar1)\
**Post date:** [30 April 2008 00:23 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/8 "2008-04-30T00:23:10Z")

</div>

Hi,

> "David A. Black" \<dblack@rubypal.com\> 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

> **···**
>
> On Wed, Apr 30, 2008 at 4:50 AM, Brian Adkins \<lojicdotcom@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 April 2008 08:47 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/9 "2008-04-29T08:47:33Z")

</div>

#..  
# Array.methods.each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^^^^^^^^^^^

make that

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Array.instance\_methods.each do |m|

sorry, my bad.

so,

class MyArray \< Array  
&nbsp;&nbsp;&nbsp;Array.instance\_methods.each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(m) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Here you go: #{super}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end  
#=\>....

g=MyArray.new [1,2,3]  
#=\> Here you go: [1, 2, 3]  
g[0]  
#=\> "Here you go: 1"  
g.first  
#=\> "Here you go: 1"  
g[-1]  
#=\> "Here you go: 3"  
g.last  
#=\> "Here you go: 3"  
g[2]  
#=\> "Here you go: 3"

kind regards -botp

> **···**
>
> From: Peña, Botp [[mailto:botp@delmonte-phil.com](mailto:botp@delmonte-phil.com)]

---

<div class="post-metadata">

**Author:** ![Fredrik](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fredrik](https://rubytalk.org/u/Fredrik)\
**Post date:** [29 April 2008 08:50 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/10 "2008-04-29T08:50:05Z")

</div>

But that doesn't work (though I don't understand why). I want it to  
always say "Here you go: ...", no matter how I retrieve an element out  
of the array.

> **···**
>
> On Apr 29, 5:36 pm, Peña, Botp \<b...@delmonte-phil.com\> wrote:
> 
> > from: Fredrik [mailto:fredj...@gmail.com]  
> > # include the same text in all my array elements. I just want to have  
> > # some fixed text attached to ANY operations I do with array elements.
> > 
> > there are other better ways, but i just extended your initial thought/code,
> > 
> > class MyArray \< Array  
> > &nbsp;&nbsp;&nbsp;Array.methods.each do |m|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(m) do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Here you go: #{super}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > #=\>...
> > 
> > g=MyArray.new [1,2,3]  
> > #=\> Here you go: [1, 2, 3]  
> > g[1]  
> > #=\> "Here you go: 2"  
> > g[0]  
> > #=\> "Here you go: 1"  
> > g.first  
> > #=\> 1  
> > g.last  
> > #=\> 3  
> > g[-1]  
> > #=\> "Here you go: 3"
> > 
> > kind regards -botp

---

<div class="post-metadata">

**Author:** ![Sandro\_Paganotti](https://avatars.discourse-cdn.com/v4/letter/s/f04885/32.png) [@Sandro\_Paganotti](https://rubytalk.org/u/Sandro_Paganotti)\
**Post date:** [29 April 2008 08:51 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/11 "2008-04-29T08:51:46Z")

</div>

Maybe is better using instance\_methods instead

class MyArray \< Array  
&nbsp;&nbsp;&nbsp;Array.instance\_methods.each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;define\_method(m) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Here you go: #{super}"  
&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;end  
end

irb(main):033:0\> g=MyArray.new [1,2,3]  
=\> Here you go: [1, 2, 3]  
irb(main):034:0\> g.first  
=\> "Here you go: 1"  
irb(main):035:0\>

> **···**
>
> On Tue, Apr 29, 2008 at 8:36 AM, Peña, Botp \<botp@delmonte-phil.com\> wrote:
> 
> > from: Fredrik [mailto:fredjoha@gmail.com]  
> > # include the same text in all my array elements. I just want to have
> > 
> > # some fixed text attached to ANY operations I do with array elements.
> > 
> > there are other better ways, but i just extended your initial thought/code,
> > 
> > class MyArray \< Array  
> > &nbsp;&nbsp;&nbsp;Array.methods.each do |m|  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(m) do  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"Here you go: #{super}"  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > &nbsp;&nbsp;&nbsp;end  
> > end  
> > #=\>...
> > 
> > g=MyArray.new [1,2,3]  
> > #=\> Here you go: [1, 2, 3]  
> > g[1]  
> > #=\> "Here you go: 2"
> > 
> > g[0]  
> > #=\> "Here you go: 1"  
> > g.first  
> > #=\> 1  
> > g.last  
> > #=\> 3  
> > g[-1]  
> > #=\> "Here you go: 3"
> > 
> > kind regards -botp
> 
> --  
> Go outside! The graphics are amazing!

---

<div class="post-metadata">

**Author:** ![Fredrik](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fredrik](https://rubytalk.org/u/Fredrik)\
**Post date:** [30 April 2008 02:45 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/12 "2008-04-30T02:45:03Z")

</div>

Ok, I got it. So Array#first is a matter of optimization. I thought it  
was syntactic sugar that the Ruby interpreter translates to Array#[]  
(0).  
But I understand now that Array#first is not there for my coding  
convenience but rather for computational speed.

Thanks for clearing it up!  
Fredrik

---

<div class="post-metadata">

**Author:** ![Fredrik](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fredrik](https://rubytalk.org/u/Fredrik)\
**Post date:** [29 April 2008 09:00 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/13 "2008-04-29T09:00:11Z")

</div>

Thanks! But how about this then:

> g.size

=\> "Here you go: 3"

That's a strange size 🙂  
So this solution solves some problems but creates new ones instead...

---

<div class="post-metadata">

**Author:** ![Albert\_Schlef](https://avatars.discourse-cdn.com/v4/letter/a/839c29/32.png) [@Albert\_Schlef](https://rubytalk.org/u/Albert_Schlef)\
**Post date:** [30 April 2008 19:10 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/14 "2008-04-30T19:10:17Z")

</div>

Fredrik wrote;

> But I understand now that Array#first is not there for my coding  
> convenience but rather for computational speed.

I'm using some other scripting language (PHP), and when it becomes known  
that method A to do something is faster than method B to do something,  
everybody turns to use method A no matter how ugly it looks. (When I say  
"method" I don't mean the object orient meaning. I simply mean "way".)

Now,

If #first was slower than #[0], \_nobody\_ would have used it!  
Having #first is nice. It has merit (clarity). So the only way to have  
it and have people use it is not to have it slower than #[0].

So on the contrary: #first is there for your convenient, not for  
computational speed.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 April 2008 09:08 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/15 "2008-04-29T09:08:45Z")

</div>

# irb\> g.size  
# =\> "Here you go: 3"

> **···**
>
> From: Fredrik [mailto:fredjoha@gmail.com]  
> #  
> # That's a strange size 🙂  
> # So this solution solves some problems but creates new ones instead...
> 
> you wanted to put text on all ops
> 
> so even
> 
> > g.last
> 
> =\> "Here you go: 3"
> 
> is already strange, right?
> 
> my suggestion is if you just want to view some output w text, just wrapped it in a text,
> 
> like so,
> 
> puts "Here you go: #{g.last}"
> 
> kind regards -botp

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 April 2008 09:22 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/16 "2008-04-29T09:22:55Z")

</div>

# From: Fredrik [[mailto:fredjoha@gmail.com](mailto:fredjoha@gmail.com)]  
# # irb\> g.size  
# # =\> "Here you go: 3"  
# # That's a strange size 🙂  
# # So this solution solves some problems but creates new ones  
# instead...  
# you wanted to put text on all ops  
# so even  
# irb\> g.last  
# =\> "Here you go: 3"  
# is already strange, right?  
# my suggestion is if you just want to view some output w text,  
# just wrapped it in a text,  
# like so,  
# puts "Here you go: #{g.last}"

ok, how about combining what we want, ie, put the text yet still returning the object, so

class MyArray \< Array  
&nbsp;&nbsp;&nbsp;Array.instance\_methods.each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(m) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r=super  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Here you go: #{r}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end  
#=\> ...

g=MyArray.new [1,2,3]  
Here you go: [1, 2, 3]  
#=\> [1, 2, 3]  
g[0]  
Here you go: 1  
#=\> 1  
g.first  
Here you go: 1  
#=\> 1  
g.last  
Here you go: 3  
#=\> 3  
g.size  
Here you go: 3  
#=\> 3  
puts "size is: #{g.size}"  
Here you go: 3  
size is: 3  
#=\> nil

is that ok?

kind regards -botp

> **···**
>
> From: Peña, Botp [[mailto:botp@delmonte-phil.com](mailto:botp@delmonte-phil.com)]

---

<div class="post-metadata">

**Author:** ![Fredrik](https://avatars.discourse-cdn.com/v4/letter/f/cab0a1/32.png) [@Fredrik](https://rubytalk.org/u/Fredrik)\
**Post date:** [29 April 2008 09:25 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/17 "2008-04-29T09:25:04Z")

</div>

I'm sorry, I think I am too vague about what I want to achieve. What I  
want is to have is an "array" that behaves exactly like an array  
EXCEPT that each element has some text attached to it. Array#size is  
not an element but Array#last is an element for example.

Maybe it's not doable in any other way than copying the text into each  
element since I would like this text to follow into Array#each,  
Array#map and all those methods.

What I am still thinking about though, is this:  
Why on earth is Array#first not calling Array#[0] ???

Fredrik

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [29 April 2008 09:41 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/18 "2008-04-29T09:41:36Z")

</div>

# I'm sorry, I think I am too vague about what I want to achieve. What I  
# want is to have is an "array" that behaves exactly like an array  
# EXCEPT that each element has some text attached to it. Array#size is  
# not an element but Array#last is an element for example.  
# Maybe it's not doable in any other way than copying the text into each  
# element since I would like this text to follow into Array#each,  
# Array#map and all those methods.

ok, then just modify those you want

eg,

class Array2 \< Array  
&nbsp;&nbsp;&nbsp;%w([] first last).each do |m|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;define\_method(m) do  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r = super  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "Here you go: #{r}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;r  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;end  
end  
#=\> ["[]", "first", "last"]  
a=Array2.new [1,2,3]  
#=\> [1, 2, 3]  
a[0]  
Here you go: 1  
#=\> 1  
a.first  
Here you go: 1  
#=\> 1  
a.size  
#=\> 3

# What I am still thinking about though, is this:  
# Why on earth is Array#first not calling Array#[0] ???

that would be calling two methods, and would be quite expensive, right?

kind regards -botp

> **···**
>
> From: Fredrik [[mailto:fredjoha@gmail.com](mailto:fredjoha@gmail.com)]

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [29 April 2008 10:59 UTC](https://rubytalk.org/t/array-first-is-not-array-0/46177/19 "2008-04-29T10:59:09Z")

</div>

Hi --

> I'm sorry, I think I am too vague about what I want to achieve. What I  
> want is to have is an "array" that behaves exactly like an array  
> EXCEPT that each element has some text attached to it. Array#size is  
> not an element but Array#last is an element for example.
> 
> Maybe it's not doable in any other way than copying the text into each  
> element since I would like this text to follow into Array#each,  
> Array#map and all those methods.
> 
> What I am still thinking about though, is this:  
> Why on earth is Array#first not calling Array#[0] ???

The real question is: why should it? They're completely separate  
methods.

Mind you, if you dig through the ruby-lang archives, you'll find Ben  
Tilly and Mathieu Bouchard patiently explaining to me the answer to  
the same question 🙂 My initial assumption was that all get and set  
operations would go through and =. They don't, though. It would  
make it very easy to do Perl-style "tie" operations if they did, but  
it would also be less efficient; in other words, not going through   
and = is an optimization.

I'm perennially working on a "tie" module, and never seem to get it  
how I want it... but I'll have another look at it and see how close  
it's getting.

David

> **···**
>
> On Tue, 29 Apr 2008, Fredrik wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!
