# What is \[\] here

**URL:** <https://www.ruby-forum.com/t/what-is-here/151080>\
**Category:** Ruby\
**Created:** [October 16, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/what-is-here/151080 "2008-10-16T15:59:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 3:59pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/1 "2008-10-16T15:59:13Z")

</div>

class User  
puts [].methods.size  
puts self.methods.size  
end

What does it mean to have [] method at a class level? Where is the  
documentation for it?

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/2 "2008-10-16T16:09:12Z")

</div>

On Oct 16, 2008, at 8:58 AM, Raj S. wrote:

> class User  
> puts [].methods.size  
> puts self.methods.size  
> end
> 
> What does it mean to have [] method at a class level? Where is the  
> documentation for it?

[] here is the empty array, not a method call. So [].methods.size just  
returns how many methods are defined for an instance of Array.

Why it’s at class level, I’ve no idea. There doesn’t seem to be any  
need to have it there.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 4:10pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/3 "2008-10-16T16:10:23Z")

</div>

class User

> puts [].methods.size  
> puts self.methods.size  
> end
> 
> What does it mean to have [] method at a class level? Where is the  
> documentation for it?

Here, [] is just an empty array literal. [].methods.size returns the  
number  
of public methods that an array has.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/4 "2008-10-16T16:15:17Z")

</div>

I should have mentioned that I picked up the code from the source code  
of named\_scope.rb ( from rails). In rails the code is

[].methods.each do |m|  
unless m =~  
/(^\_\_|^nil?|^send|^object\_id$|class|extend|^find$|count|sum|average|maximum|minimum|paginate|first|last|empty?|respond\_to?)/  
delegate m, :to =\> :proxy\_found  
end  
end

[].methods was throwing me off.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 4:16pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/5 "2008-10-16T16:16:51Z")

</div>

On Thu, Oct 16, 2008 at 3:58 PM, Raj S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class User  
> puts [].methods.size  
> puts self.methods.size  
> end
> 
> What does it mean to have [] method at a class level? Where is the  
> documentation for it?

[] is a literal for a new empty Array.

irb(main):001:0\> [].object\_id  
=\> -606116168  
irb(main):002:0\> [].object\_id  
=\> -606126028  
irb(main):003:0\> [].object\_id  
=\> -606134558  
irb(main):004:0\> [].class  
=\> Array

Jesus.

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/6 "2008-10-16T16:14:30Z")

</div>

2008/10/16 Raj S. \<neeraj.jsr@…\>:

> What does it mean to have [] method at a class level? Where is the  
> documentation for it?

It is the empty array. Just type

[].class =\> Array  
[].size =\> 0

Regards,

Serabe

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 5:06pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/7 "2008-10-16T17:06:19Z")

</div>

[] is a way to declare an empty array. It’s a nice alternative to  
Array.new.

puts [].methods.size

will print the number of methods on a new, empty Array instance. On my  
Ruby  
install (1.8.6 p114 on Mac OS X 10.5.5), the output is 121.

Craig

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 5:15pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/8 "2008-10-16T17:15:08Z")

</div>

On 16.10.2008 16:13, Serabe wrote:

> 2008/10/16 Raj S. \<neeraj.jsr@…\>:
> 
> > What does it mean to have [] method at a class level? Where is the  
> > documentation for it?
> 
> It is the empty array. Just type
> 
> [].class =\> Array  
> [].size =\> 0

It is not _the_ empty Array but _an_ empty Array. Things like [], “”  
and ‘’ are actually constructors for new objects. This is important to  
keep in mind. Especially people coming from Java might get the wrong  
impression, that

a = “foo”  
b = “foo”

will create two variables pointing to the same object. In reality,  
there are two variables pointing to two different objects. You can  
easily see that from

$ ruby -e ‘4.times do p [[],"",’’’’].map {|o|o.object\_id} end’  
[134314000, 134313990, 134313980]  
[134313890, 134313880, 134313870]  
[134313790, 134313780, 134313770]  
[134313690, 134313680, 134313670]

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![dorelal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/3da27b/32.png) [@dorelal](https://www.ruby-forum.com/u/dorelal)\
**Post date:** [October 16, 2008, 5:23pm UTC](https://www.ruby-forum.com/t/what-is-here/151080/9 "2008-10-16T17:23:57Z")

</div>

> puts [].methods.size

A more explicit alternative is

puts Array.instance\_methods.size

– Mark.
