Pass an array to a new instance of a class.

J

Jonathan Allen

I am having trouble passing values to an array of a new instance of the
class Item that I have created. The following codse does not fille the
flags array with the value 'container'.


class Item
attr_accessor :inum, :name, :description, :slot, :flags, :items

def initialize( inum, name, description, slot, flags, items )
@inum = inum
@name = name
@description = description
@slot = slot
@flags = []
@items = []
end

def is_container
return true if flags.include?(container)
end
end


Item.new("0007", "crate", "A large crate.\r\n", ":none", "container",
"")


However when I use the code below, I am able to add the value
'container' to the flags array.


$rooms[("0001")].item("crate").flags << "container"


My question is, how do I modify the first bit of code to allow passing
values to the array of the new instance of the class Item?
 
J

Jonathan Allen

Robert said:
If you do not always pass an Array you can do

@items = []
@items << items

Thanks Robert. I didn't think about pushing the items into the array
from within the actual code for the class Item itself.

This worked just the way I was hoping. Thanks again!
 
J

Jesús Gabriel y Galán

=A0def is_container
=A0 =A0return true if flags.include?(container)
=A0end
end

Apart from all the other suggestions, you should change the container
above to "container".

Jesus.
 
R

Robert Klemme

Robert said:
If you do not always pass an Array you can do

@items = []
@items<< items

Thanks Robert. I didn't think about pushing the items into the array
from within the actual code for the class Item itself.

This worked just the way I was hoping. Thanks again!

Note though that the code above only appends a single Array to the Array
- it does not "push items into the array"! For that you need #concat as
I have shown.

Kind regards

robert
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top