Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Ruby

Ryan Sutherland
PLUS
Ryan Sutherland
Courses Plus Student 5,051 Points

Adding an Incremental Value to an Array?

Hello all,

I'm working on the Extra Credit for the Address Book course. And what I'm trying to do is assign each contact a unique ID number so that it would be easy to edit/delete the correct contact without mistakenly affecting any contacts that share similar search criteria.

The problem I'm running into is initially creating the Class that creates the ID number. Here's what I have:

ruby
class Id_number
    attr_writer :id_number

#Create and initialize an empty array

    def initialize
        @id_numbers = []
    end

#Create a unique ID by counting the number of items currently in the array, incrementing by 1, then adding that value to the array

    def create_id
        id_number = id_numbers.count + 1
        id_numbers.push(id_number)
    end
end

#New Object
id_number = Id_number.new

#Create ID
id_number.create_id

#Print ID number to screen
puts id_numbers

However, I keep getting an undefined local variable in my create_id method. I don't think I want to pass in an argument, because I don't have an argument. I'm trying to create one.

I haven't even gotten to trying to tie this number to a specific entry in my Address Book, and I'm already stressing. Please help?

1 Answer

Kourosh Raeen
Kourosh Raeen
23,733 Points

Hi Ryan - You need to either add the following line:

attr_reader :id_numbers

or use @id_numbers instead of id_numbers in the create_id method.

Ryan Sutherland
Ryan Sutherland
Courses Plus Student 5,051 Points

Kourosh,

Thank you for that answer. Adding attr_reader worked. However, now I'm trying to print out both the value of id_number and the id_numbers array, and all I'm getting is its location in memory. Any thoughts?

Kourosh Raeen
Kourosh Raeen
23,733 Points

Try this:

class Id_number
    attr_writer :id_number
    attr_reader :id_numbers

#Create and initialize an empty array

    def initialize
        @id_numbers = []
    end

#Create a unique ID by counting the number of items currently in the array, incrementing by 1, then adding that value to the array

    def create_id
        id_number = id_numbers.count + 1
        id_numbers.push(id_number)
    end
end

#New Object
id_number = Id_number.new

#Create ID
id_number.create_id
id_number.create_id

#Print ID number to screen
puts id_number.id_numbers
Ryan Sutherland
Ryan Sutherland
Courses Plus Student 5,051 Points

Kourosh, you're a genius. Thank you!

...now let's hope I can figure out the rest of this...