From: "Aaron D. Gifford" Date: 2010-02-15T16:08:30+09:00 Subject: Re: Compare and delete element from an array On Sun, Feb 14, 2010 at 10:48 PM, Greg Ma wrote: > >> Note that the first version with a "puts" at the end of the delete_if >> block failed to remove element "two", but reordering the operation in >> the second version worked. >> > Even if i remove the puts, it still doesnt remove anything. > > def remove_tags(array_of_tags) > if !array_of_tags.empty? > array_of_tags.each do |tag| > tags.delete_if { |x| x.name == tag } > end > end > end You call the "delete_if()" method on the "tags" variable--where is tags defined? You've fixed one bug (and it was indeed a bug). If your code isn't working now, it likely has to do with the scope where tags is defined. In your original post you didn't mention getting a NameError "undefined local variable or method `tags'" exception, so unless your code (or whatever code executes your code) rescues such an exception silently, I assume that "tags" IS in fact defined somehow/somewhere. Since I don't know how your "tags" array or Student class works, I contrived something that's probably very, very different, but may give you an idea where to look in your own code for your bug: ### CODE STARTS ### class Tag def initialize(name) @name = name end attr_accessor :name def to_s "TAG(#{@name})" end end class Student def initialize() @tag_list = [] end ## Add a single tag to the Student: def add_tags(array_of_tags) array_of_tags.each do |tag_name| @tag_list << Tag.new(tag_name) end end ## Remove multiple tags from the Student: def remove_tags(array_of_tags) array_of_tags.each do |tag_name| @tag_list.delete_if { |x| x.name == tag_name } end end def to_s "STUDENT(" + @tag_list.map{ |tag| tag.to_s }.join(",") + ")" end end jane_doe = Student.new jane_doe.add_tags(["intelligent", "lazy", "geek", "irresponsible", "nerd", "smart"]) puts "Jane has these tags:" puts jane_doe.to_s tags_to_remove = [ "lazy", "irresponsible" ] puts "Removing tags with these names:" puts " " + tags_to_remove.join(",") ## Remove the tags: jane_doe.remove_tags(tags_to_remove) puts "After removal, Jane has these tags:" puts jane_doe.to_s ### CODE ENDS ### ### SAMPLE OUTPUT: ### Jane has these tags: STUDENT(TAG(intelligent),TAG(lazy),TAG(geek),TAG(irresponsible),TAG(nerd),TAG(smart)) Removing tags with these names: lazy,irresponsible After removal, Jane has these tags: STUDENT(TAG(intelligent),TAG(geek),TAG(nerd),TAG(smart)) ### OUTPUT ENDS ### In the above contrived sample, notice that the Student#remove_tags() method is a bit different than your method. That's because I defined the equiv. of your "tags" variable as a Student class instance variable "@tag_list" instead. Also I omitted the "if" statement "if !@tag_list.empty?" (or "if !tags.empty?" in your code) because the each method will not execute the block at all if the array is empty to begin with--it's redundant and not needed. I hope this gives you an idea of where in your own code to look to debug it. Aaron out.