Skip to content

Instantly share code, notes, and snippets.

@overwine
Last active May 23, 2016 21:11
Show Gist options
  • Save overwine/c4f5263a326f55efeb7cc96a49be4086 to your computer and use it in GitHub Desktop.
Save overwine/c4f5263a326f55efeb7cc96a49be4086 to your computer and use it in GitHub Desktop.
class Person
#have a first_name and last_name attribute with public accessors
#attr_accessor
public
attr_accessor :first_name, :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 << 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
new_array = @@people.select! { |people_last_name| people_last_name == last_name }
#search the `people` class attribute for instances with the same `last_name`
#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`
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")
# Should print out
# => John Smith
# => Jane Smith
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment