Jamey Cribbs <jcribbs / netpromi.com> wrote: > Sean Hussey wrote: >> Hi everone, >> >> I'm not quite sure what the best way to go about this is. I have a >> file with one user per line. Each line contains ID, username, phone, >> etc. I split all these up into variables and then either create a >> new user or update an existing user: >> >> File.open("users.txt", "r") do |file| >> file.each_line { |line| >> id, username, phone, lots, more, variables = >> line.chomp.split(/\|/); if user = ldap.get_entry(username) >> # Existing user. Check for update. >> user.update(I, hate, passing, all, of, the, variables, that, >> were, just, split) >> else >> # New user. Create! >> user.new(Same, here, see, what, I mean?) >> end >> } >> end >> >> Should I put it all into an array? Hash? Would that make the split >> line huge but save space on the method calls? How would you go about >> this? > > Well, if #update and #new are looking for the variables in the same > order that they are in the text file, you could do: > > > File.open("users.txt", "r") do |file| > file.each_line { |line| > rec = line.chomp.split(/\|/); > if user = ldap.get_entry(rec[1]) > # Existing user. Check for update. > user.update(*rec) > else > # New user. Create! > user.new(*rec) > end > } > end Or even File.open("users.txt", "r") do |file| file.each_line { |line| rec = line.chomp.split(/\|/) user.send(ldap.get_entry(rec[1]) ? :update : :new, *rec) } end :-) robert