On Sun, Oct 9, 2011 at 5:44 PM, Kevin E. <kellwood / gmail.com> wrote:

> Hi All
>
> I am try to get up to speed on several things at once; namely, OO and
> also Ruby.  I have written a bunch or Ruby scripts to perform various
> text processing tasks but I think I want to start learning something
> about OO Programming.  I do have the book "Programming Ruby" and it is a
> great help.
>
> My question is related to writing a class for something like a polygon.
> It can be described by a few integer and string properties. So I could
> do something like this snippet:
>
> class Zone
>
>    attr_accessor :type, :length
>
>    def initialize(type, length)
>      @type = type
>      @length = length
>    end
>
> end
>
> testZ = Zone.new("Rad", 20.1)
>
> Here is what I don't know how to do.  How do I add an array of integers
> to this class?  It would need to be of variable length.
>
> Sorry for the basic question?
>
> Kev
>
> --
> Posted via http://www.ruby-forum.com/.
>
>
Give it an accessor and then initialize the associated instance variable to
an array.


class Zone

  attr_accessor :type, :length, :integers

  def initialize(type, length)
    @type = type
    @length = length
    @integers = Array.new
  end

end

test_z = Zone.new("Rad", 20.1)
test_z.integers # => []
test_z.integers << 1
test_z.integers # => [1]
test_z.integers << 2
test_z.integers # => [1, 2]


In terms of the integers part, you can't specify the array should contain
integers, I've named it integers to indicate that's what it is, but really
you could put anything in there.