Skip to content

Instantly share code, notes, and snippets.

@korutsuru
Created March 2, 2011 08:23
Show Gist options
  • Save korutsuru/850637 to your computer and use it in GitHub Desktop.
Save korutsuru/850637 to your computer and use it in GitHub Desktop.
#variables________________________________________________
request=nil
#methods__________________________________________________________________
def mainmenu
puts 'select an option please
1.- new user
2.- delete user
3.- consult user
4.- exit
type a number: '
option=gets.chomp
choose = case option
when '1' then create_new_user
when '2' then
puts 'please type the name of the user'
request=gets.chomp
delete_user(request)
when '3' then
puts 'please type the name of the user'
request=gets.chomp
consult_user(request)
when '4' then Process.exit
end
end
def create_new_user
p 'name'
name=gets.chomp
p'age'
age=gets.chomp
p'phone'
phone=gets.chomp
p'address'
address=gets.chomp
File.open('data.csv','a') do |file|
file.puts("#{name}, #{age}, #{phone}, #{address}") end
mainmenu
end
def delete_user(user)
myarray=[]
string=""
File.open('data.csv','r') do |myfile|
myfile.each {|line|
myarray << line.split(/(?<=,)\s/)
}
myarray=myarray.select{|x| x unless x.first.nil? || x.first=="\n"}
myarray.each_index {|index|
myarray.delete_at(index) if myarray[index].include?("#{user},")
}
p myarray
end
puts "lengthy= #{myarray.length}"
File.open('data.csv','w') do |file|
myarray.each {|innerarray|
innerarray.each{|element|
element.end_with?(',')? file.print("#{element} ") : file.print(element)
}
}
end
mainmenu
end
def consult_user(user)
File.open('data.csv','r') do |myfile|
myfile.each {|line|
puts line if line.start_with?(user)} end
mainmenu
end
#__________________________________________________________________________
#______BODY________________________________________________________________
mainmenu
#__________________________________________________________________________
=begin
data.csv
eduardo, 23, 3124567, sanisidro 23
arturo, 32, 3245678, la calua 78
jorge, 45, 2345678, javier mina 4
=end
eduardo 23 3124567 sanisidro 23
arturo 32 3245678 la calua 78
jorge 45 2345678 javier mina 4
josue 5000000 310000000 jerusalem 111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment