On Sat, Dec 3, 2011 at 11:13 AM, Dani Dani <[email protected]> wrote:
> want to prevent it being added twice.
Very simplistically --
class PotentialCourseStudents < Array
def << (customer_student)
raise ArgumentError, "already exists" if self.include?(customer_student)
self.push(customer_student)
## alternately, ignore duplicates silently
# self.push(customer_student) unless self.include?(customer_student)
end
end
Note that there are more ways to add to an array than just `<<` so
you need to evaluate whether this is going to work for you :-)
Also, Ruby's stdlib has a "Set" which is an *unordered* collection, but
which does not allow duplicates. So if order's not important, you could
just go with
@potential_course_students = Set.new
Something to think about...
HTH,
--
Hassan Schroeder ------------------------ [email protected]
http://about.me/hassanschroeder
twitter: @hassan
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.