0

So I am creating a class in ruby where in I will be able to insert my data on a text file and then read, find from it but I am stuck on delete as well update/edit.

Basically I created a method called "find" and I made it as a reference on my "delete" method.

def find(keyword="")
  if keyword
    person = People.read_people
    found = person.select do |pip|
      pip.name.downcase.include?(keyword.downcase) ||
        pip.age.downcase.include?(keyword.downcase) ||
        pip.country.downcase.include?(keyword.downcase)
    end
    found.each do |person|
      puts person.name + " | " + person.age + " | " + person.country
    end
  else
    puts "find using a key phrase eg. 'find sam' \n\n"
  end
end


def list
  puts "\nListing People \n\n".upcase
  people = People.read_people
  people.each do |person|
    puts person.name + " | " + person.age + " | " + person.country
  end
end


def delete(keyword="")
  if keyword
    person = People.read_people
    found = person.select do |pip|
      pip.name.downcase.include?(keyword.downcase) ||
        pip.age.downcase.include?(keyword.downcase) ||
        pip.country.downcase.include?(keyword.downcase)
    end
    person.delete(found)
  else
    puts "find using a key phrase eg. 'find josh' \n\n"
  end
end

As you can see I was trying to delete the supplied keyword from the array (w/c was save on a text file) via class method called read_people. Here's how it looks like:

def self.read_people
  # read the people file
  # return instances of people
  people = []
  if file_usable?
    file = File.new(@@filepath, 'r')
    file.each_line do |line|
      people << People.new.import_line(line.chomp)
    end
    file.close
  end
  return people
end

def import_line(line)
  line_array = line.split("\t")
  @name, @age, @country = line_array
  return self
end

How can I fix this and delete the found item via keyword?

See the actual codes here: https://repl.it/repls/VastWildFact

3
  • Just to be clear, you want to delete the person from the file via keyword? Commented Nov 14, 2018 at 14:51
  • I was trying to delete the supplied keyword from the array Commented Nov 14, 2018 at 14:51
  • @GavinMiller: correct delete it from the text file. and then when the List command was executed it must not appear anymore on the list. Commented Nov 14, 2018 at 14:54

2 Answers 2

1

Change

person.delete(found)

to

person -= found # Equivalent to person = person - found

It should work as per https://ruby-doc.org/core-2.2.0/Array.html#method-i-2D

ary - other_ary → new_ary

Returns a new array that is a copy of the original array, removing any items that also appear in other_ary. The order is preserved from the original array.

It compares elements using their hash and eql? methods for efficiency.

Example: [ 1, 1, 2, 2, 3, 3, 4, 5 ] - [ 1, 2, 4 ] #=> [ 3, 3, 5 ]


Another solution is to use reject as follows:

person.reject! do |pip|
  pip.name.downcase.include?(keyword.downcase) ||
    pip.age.downcase.include?(keyword.downcase) ||
    pip.country.downcase.include?(keyword.downcase)
end
Sign up to request clarification or add additional context in comments.

8 Comments

For some reason it doesn't delete the item on the text file.
You did not ask to delete the item from the text file :) I was trying to delete the supplied keyword from the array
yes. By default i'll get an array which was read from a text file.
My code is updating the array called person. This array was initially loaded from a file but it has nothing to do with file afterwards. Any changes in the array won't affect the file!
Updated the description sorry for misunderstanding.
|
1

Basically you're going to want an export_people and write_people method that'll look something like this:

def self.export_people(people)
  people.map do |person|
    [person.name, person.age, person.country].join("\t")
  end
end

def self.write_people(people)
  File.new(@@filepath, 'w') do |f|
    f.write(export_people(people))
  end
end

# Usage:
Person.write_people(array_of_people)

With the above code, you'd call the modified delete method as detailed in Tarek's answer, and then Person.write_people(array_of_people) to write back to the file.

4 Comments

I already have a writing (adding) function. I just want to delete the item based on keyword supplied.
@MarcSolva before you save delete the old file :) If the old file has 3 people and you deleted one of them then your array has 2 people. You need to delete the content of the original file (the 3 people) and then write the only remaining 2! otherwise you will end up appending the remaining 2 to the original 3.
Hmm. sorry I am a beginner I am not still following would you be so kind and show me an updated version on how to execute the codes?
@MarcSolva you have a save method in your code, but it's only saving single people, not an array of people. In order to save the array of people, you have to re-write the file with only the people you want saved. Your code will be roughly: load all people -> find by keyword -> delete found -> write all people to file. Taking you much further than this without you learning how to do it on your own won't help you in the long run ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.