Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby "Undefined Method" for class method

Ruby beginner struggling to simply print out the value of this @@people hash to the console

class Person
  #have a first_name and last_name attribute with public accessors
  attr_accessor :first_name 
  attr_accessor :last_name

  #have a class attribute called `people` that holds an array of objects
  @@people = []

  #have an `initialize` method to initialize each instance
  def initialize( first_name, last_name )#should take 2 parameters for first_name and last_name
    #assign those parameters to instance variables
    @first_name = first_name
    @last_name = last_name
    #add the created instance (self) to people class variable
    @@people.push self
  end

  #have a `search` method to locate all people with a matching `last_name`
  def self.search( last_name )
    #accept a `last_name` parameter
    @search_name = last_name
    #search the `people` class attribute for instances with the same `last_name`
    @@people.select {|last_name, value| value == "Smith"}.to_s
    #return a collection of matching instances
  end

  #have a `to_s` method to return a formatted string of the person's name
  def to_s
    #return a formatted string as `first_name(space)last_name`
    self.each { |first_name,last_name| print "#{first_name} #{last_name}" }
  end

  def print_hash
    p @@people
  end

end

p1 = Person.new("John", "Smith")
p2 = Person.new("John", "Doe")
p3 = Person.new("Jane", "Smith")
p4 = Person.new("Cool", "Dude")

#puts Person.search("Smith")
puts Person.print_hash
# Should print out
# => John Smith
# => Jane Smith
like image 825
MattO Avatar asked Mar 13 '16 00:03

MattO


People also ask

What does undefined method mean in Ruby?

Undefined method call created a NoMethodError. This is a typical Ruby error that indicates that the method or attribute you are attempting to call on an object has not been declared.

How do you define a class method in Ruby?

There are two standard approaches for defining class method in Ruby. The first one is the “def self. method” (let's call it Style #1), and the second one is the “class << self” (let's call it Style #2). Both of them have pros and cons.

What is NoMethodError in Ruby?

Ruby's NoMethodError class. NoMethodError is raised when a method is called on a receiver which doesn't have it defined and also fails to respond with method_missing : "creature".rawr.

Which error is generated when we try to access undefined method?

Accessing a non-existing property does not throw an error. The problem appears when trying to get data from the non-existing property, which is the most common undefined trap, reflected in the well-known error message TypeError: Cannot read property <prop> of undefined .


1 Answers

You defined print_hash as an instance method. To be able to call it like People.print_hash define it this way: def self.print_hash

like image 71
Lucas Moulin Avatar answered Oct 10 '22 06:10

Lucas Moulin