Forum: Ruby what is [] here

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
934f858e451cf9b771996b2940cd696b?d=identicon&s=25 Raj Singh (dorelal)
on 2008-10-16 15:59
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?
A61ecce13ed142622f24a5ca3a123922?d=identicon&s=25 Matthew Moss (Guest)
on 2008-10-16 16:09
(Received via mailing list)
On Oct 16, 2008, at 8:58 AM, Raj Singh 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.
53581739a445ad78250a676dabddf55f?d=identicon&s=25 James Coglan (Guest)
on 2008-10-16 16:10
(Received via mailing list)
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.
7b8e1744137ecbad51b24ab2d169779b?d=identicon&s=25 Serabe (Guest)
on 2008-10-16 16:14
(Received via mailing list)
2008/10/16 Raj Singh <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
934f858e451cf9b771996b2940cd696b?d=identicon&s=25 Raj Singh (dorelal)
on 2008-10-16 16:15
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.
E088bb5c80fd3c4fd02c2020cdacbaf0?d=identicon&s=25 Jesús Gabriel y Galán (Guest)
on 2008-10-16 16:16
(Received via mailing list)
On Thu, Oct 16, 2008 at 3:58 PM, Raj Singh <neeraj.jsr@gmail.com> 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.
B14575f0ca69f10938fdd67e7156e0e1?d=identicon&s=25 Craig Demyanovich (Guest)
on 2008-10-16 17:06
(Received via mailing list)
[] 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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2008-10-16 17:15
(Received via mailing list)
On 16.10.2008 16:13, Serabe wrote:
> 2008/10/16 Raj Singh <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
134ea397777886d6f0aa992672a50eaa?d=identicon&s=25 Mark Thomas (Guest)
on 2008-10-16 17:23
(Received via mailing list)
> puts [].methods.size

A more explicit alternative is

  puts Array.instance_methods.size

-- Mark.
This topic is locked and can not be replied to.