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

Tomasz Necek
PLUS
Tomasz Necek
Courses Plus Student 7,028 Points

undefined method 'push' for "Andrew":string (NoMethodError)

Why do I get this error? This code is the same as in the excercise (part of phone book code from the course)

def ask(question, kind = "string")
    puts question + ''
    answer = gets.chomp
    return answer
end

def add_contact
    contact = {"name" => '', "phone" => []}
    contact["name"] = ask("Write your name here: ")
    answer = ''  
    contact["name"].push(answer)
end

undefined method 'push' for "Andrew":string (NoMethodError)

  • push method is NOT working,

I put "Andrew" as the name

Can you help me understand why ?? Thank you !

Can you link to the video - that way, I can call up my Workspace and understand what your issue is.

I don't think a string has a .push() method. I think you should be pushing your new contact onto a contacts array using .push(). But I need the video/course link to help further!

Steve.

5 Answers

Hi Tomasz,

My Workspace is a little further on than yours but shows a couple of points. Let's start with the finished code - don\t worry about this; your code will catch up with it:

def add_contact
  contact = { "name" => "", "phone_numbers" => [] }
  contact["name"] = ask("What is the person's name?")
  answer = ""
  while answer != "n"
    answer = ask("Do you want to add a phone numbers? (y/n)")
    if answer == "y"
      phone = ask("Enter a phone number:")
      contact["phone_numbers"].push(phone)
    end
  end
  return contact
end

Now, let's focus on the .push method that you were having problems with. Working backwards at the top of this method, add_contact we create a new contact which consists of a name (a string) and phone_numbers, an array which is empty. We then store a string in the name variable. Next, we add any number of phone numbers. While we are adding, at each number added, we .push that number onto the array.

So, I don't think you should be pushing anything to a string as you are doing. It might be worth just revisiting the video to make sure you've got the right code written.

Steve.

No, you can't push to a string. There were three common basic methods that operated on stacks and queues back in the day. Push, Pop and Peek. Push put something on the stack, Pop removed and returned the top of the stack and Peek let you see what was o the top of the stack. This is used in machine code and that translates into managing arrays and lists etc.

You can add to a string; indeed there are many operations you can use on a string. Just not push. :smile:

Tomasz Necek
PLUS
Tomasz Necek
Courses Plus Student 7,028 Points

Thank you! :) I know the code of the exercise. I just wanted to do something else but I missed that I cannot push name to a String.