From: Sam Duncan Date: 2013-07-09T15:43:23+09:00 Subject: Re: Well-Grounded Rubyist -- Person class implementation in chapter 4 question This is a multi-part message in MIME format. --------------050600040007080501010508 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 09/07/13 18:14, Arslan Farooq wrote: > Hi, > > I am new to programming and new to Ruby. > > I have been studying Ruby using David Black's book, and I need bit of > help with this: > > Code in listing 4.11, 4.12 and 4.13 needs to be pieced together (I > think) to do something useful. However it's a bit confusing... whether > they are meant to just studied as examples separately, or whether I am > supposed to piece them together. Please have a look at the code: > > https://gist.github.com/arslanfarooq/54ac5aa7796385e4a063 > > If you could explain a bit what's going on in 4.13 that would be great. > > On my computer I have combined the code in one file "person_class.rb" > and I am getting this error: > > person_class.rb:17:in `has_friends': undefined method `<<' for > nil:NilClass (NoMethodError) from person_class.rb:43:in `
' > Looks like your Person class initialises its @hobbies and @friends instance variables with things that don't come from anywhere class Person PEOPLE = [] attr_reader :name, :hobbies, :friends def initialize(name) @name = name @hobbies = *hobbies* <-- where do these come from? @friends = *friends* PEOPLE << self end def has_hobbies(hobby) @hobbies << hobby end def has_friends(friend) @friends << friend end I expect those are supposed to be arrays? In which case the << operator would be valid as a push in your has_hobbies/ has_friends methods. Sam --------------050600040007080501010508 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit On 09/07/13 18:14, Arslan Farooq wrote:
Hi,

I am new to programming and new to Ruby.

I have been studying Ruby using David Black's book, and I need bit of
help with this:

Code in listing 4.11, 4.12 and 4.13 needs to be pieced together (I
think) to do something useful. However it's a bit confusing... whether
they are meant to just studied as examples separately, or whether I am
supposed to piece them together. Please have a look at the code:

https://gist.github.com/arslanfarooq/54ac5aa7796385e4a063

If you could explain a bit what's going on in 4.13 that would be great.

On my computer I have combined the code in one file "person_class.rb"
and I am getting this error:

person_class.rb:17:in `has_friends': undefined method `<<' for
nil:NilClass (NoMethodError) from person_class.rb:43:in `<main>'


Looks like your Person class initialises its @hobbies and @friends instance variables with things that don't come from anywhere

class Person
  PEOPLE = []
    attr_reader :name, :hobbies, :friends
   
    def initialize(name)
        @name = name
        @hobbies = hobbies   <-- where do these come from?
        @friends = friends
        PEOPLE << self
    end
   
    def has_hobbies(hobby)
        @hobbies << hobby
    end
   
    def has_friends(friend)
        @friends << friend
    end

I expect those are supposed to be arrays? In which case the << operator would be valid as a push in your has_hobbies/ has_friends methods.

Sam

--------------050600040007080501010508--