From: "Aaron D. Gifford" Date: 2010-02-15T09:50:56+09:00 Subject: Re: Compare and delete element from an array On Sun, Feb 14, 2010 at 4:45 PM, Greg Ma wrote: > def remove_tags(array_of_tags) >    if !array_of_tags.empty? >      array_of_tags.each do |tag| >        tags.delete_if { >          |x| x.name == tag >          puts x.name + "-"+ x.name.length.to_s >          puts tag + "-"+ tag.length.to_s >          puts "-----" >          } >      end >      puts tags >    end >  end Move the line: x.name == tag to the end of the block: def remove_tags(array_of_tags) if !array_of_tags.empty? array_of_tags.each do |tag| tags.delete_if {|x| puts x.name + "-"+ x.name.length.to_s puts tag + "-"+ tag.length.to_s puts "-----" x.name == tag } end puts tags end end The Array#delete_if() method only removes array elements IF the block evaluates to true. With the "x.name == tag" test positioned at the beginning of the block, it is effectively ignored, since the last expression in the block will be the block's return value. You were returning whatever the 'puts "-----"' method returns--nil in this case, which is considered "false" and so no elements would ever be deleted. By moving the "x.name == tag" expression to the bottom of the block, you will return a boolean result of the comparison. Here's another example of this behavior: irb(main):001:0> ["one", "two", "three", "four", "five"].delete_if{|x| x=="two";puts x} one two three four five => ["one", "two", "three", "four", "five"] irb(main):002:0> ["one", "two", "three", "four", "five"].delete_if{|x| puts x;x=="two"} one two three four five => ["one", "three", "four", "five"] 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. Aaron out.