What does << do?

Reading a sample program that says:
@rooms << Room.new(reference, name, description, connections)

What does the “<<” part do? Thanks!

Hi,

imho << reads Room.new(reference, name, description, connections) into
@rooms

Regards

Sebastian B.
Sent with Sparrow

It adds an instance of Room to the @rooms array

It places a new room in @rooms. Imagine this

target <[gun]< bullet

The answer is, “it depends on what @rooms is”. In Ruby, (almost)
everything’s an object, so when you write “@rooms << …”, you’re
actually invoking the “<<” method on “@rooms”.

If @rooms is an Array, for example, then << will append to the end of
the array, which is probably what’s happening here.

~ jf

John F.
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: http://stackoverflow.com/users/75170/

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs