undefined method `push' for Array:Class

Discussion in 'Ruby' started by Amanda .., Aug 25, 2008.

  1. Amanda ..

    Amanda .. Guest

    hi there, I'm trying to add objects onto the end of an array and I'm
    getting a NoMethodError with the message "undefined method `push' for
    Array:Class". This makes no sense to me as "push" should be defined for
    an Array...?

    Any help would be great, thanks!
    --
    Posted via http://www.ruby-forum.com/.
     
    Amanda .., Aug 25, 2008
    #1
    1. Advertising

  2. Amanda ..

    Xavier Noria Guest

    On Mon, Aug 25, 2008 at 9:45 PM, Amanda .. <> wrote:

    > hi there, I'm trying to add objects onto the end of an array and I'm
    > getting a NoMethodError with the message "undefined method `push' for
    > Array:Class". This makes no sense to me as "push" should be defined for
    > an Array...?


    Array#push is an _instance_ method of the Array class, you need to
    invoke it on array objects.
     
    Xavier Noria, Aug 25, 2008
    #2
    1. Advertising

  3. On Monday 25 August 2008, Amanda .. wrote:
    > hi there, I'm trying to add objects onto the end of an array and I'm
    > getting a NoMethodError with the message "undefined method `push' for
    > Array:Class". This makes no sense to me as "push" should be defined for
    > an Array...?
    >
    > Any help would be great, thanks!


    It seems your code is something like

    Array.push 'a'

    This doesn't work because push is an instance method of the Array class, that
    is: you need to call it on an individual array (an instance of class Array),
    not on the Array class itself. This is how you should do it:

    a = Array.new

    a.push 'string'

    or

    [1,2,3].push 4

    (the syntax [1,2,3] creates an array, instance of class Array, containing the
    items 1, 2 and 3).

    I hope this helps

    Stefano
     
    Stefano Crocco, Aug 25, 2008
    #3
  4. Amanda ..

    Amanda .. Guest

    Stefano Crocco wrote:
    > It seems your code is something like
    >
    > Array.push 'a'
    >
    > This doesn't work because push is an instance method of the Array class,
    > that is: you need to call it on an individual array (an instance of class
    > Array), not on the Array class itself. This is how you should do it:
    >
    > a = Array.new
    >
    > a.push 'string'
    >
    > I hope this helps
    >
    > Stefano


    Thanks a lot, that's exactly what I needed to know :)
    --
    Posted via http://www.ruby-forum.com/.
     
    Amanda .., Aug 25, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. XyZaa
    Replies:
    0
    Views:
    604
    XyZaa
    Jul 19, 2007
  2. Daniel Berger

    Testing array.push(array)

    Daniel Berger, Nov 9, 2005, in forum: Ruby
    Replies:
    2
    Views:
    137
    Daniel Berger
    Nov 9, 2005
  3. Replies:
    5
    Views:
    170
    YANAGAWA Kazuhisa
    Mar 8, 2006
  4. samppi
    Replies:
    27
    Views:
    553
    David A. Black
    Dec 5, 2007
  5. VK
    Replies:
    45
    Views:
    674
    Dr John Stockton
    Sep 12, 2006
Loading...

Share This Page