undefined method `push' for Array:Class

A

Amanda ..

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!
 
X

Xavier Noria

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.
 
S

Stefano Crocco

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
 
A

Amanda ..

Stefano said:
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 :)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top