Jamey Cribbs
11/9/2006 7:40:00 PM
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
Jamey
Confidentiality Notice: This email message, including any attachments, is for the sole use of the intended recipient(s) and may contain confidential and/or privileged information. If you are not the intended recipient(s), you are hereby notified that any dissemination, unauthorized review, use, disclosure or distribution of this email and any materials contained in any attachments is prohibited. If you receive this message in error, or are not the intended recipient(s), please immediately notify the sender by email and destroy all copies of the original message, including attachments.