PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 10, 2022

[FIXED] Why is my filter method not removing some elements that should be removed?

 November 10, 2022     array-unique, proc, ruby     No comments   

Issue

I am trying to create a method called filer_out! that takes in an array and a proc, and returns the same array but with every element that returns true when it is run through the proc, with the caveat being we can't use Array#reject!

I wrote this:

def filter_out!(array, &prc)
    array.each { |el| array.delete(el) if prc.call(el)}
end

arr_2 = [1, 7, 3, 5 ]
filter_out!(arr_2) { |x| x.odd? }
p arr_2

but when I run the code, what prints out is:

[7, 5]

even though the answer should be:

[]

Upon review of the solution I see Array#uniq was used first:

def filter_out!(array, &prc)
    array.uniq.each { |el| array.delete(el) if prc.call(el) }
end

arr_2 = [1, 7, 3, 5 ]
filter_out!(arr_2) { |x| x.odd? }
p arr_2

and the correct output was displayed:

[]

So I guess what my question is, why do you have to use Array#uniq in order to get the correct solution? thanks for your help!


Solution

The problem here is the method delete modify the original array. Here the deal if you put some information out:

def filter_out!(array, &prc)
  array.each.with_index do |el, i|
    p "Current index #{i}"
    p "Current array #{array}"
    p "Current element #{el}"
    array.delete(el) if prc.call(el)
  end
end

arr_2 = [1, 7, 3, 5 ]
filter_out!(arr_2) { |x| x.odd? }
# Output:
#"Current index 0"
# "Current array [1, 7, 3, 5]"
# "Current element 1"
# "Current index 1"
# "Current array [7, 3, 5]"
# "Current element 3"

Explain:

  • The first time, the element is 1, after it's deleted the array is [7, 3, 5]
  • The second time, index in the iteration is 1, it gets the current element with this index in the current array, in this case, is 3 not 7 and delete it, after deleting the array is [3, 5]
  • After two times it stops iteration because the current index is out of range of the current array

By using uniq you get the right result because array.uniq it creates a copy of the original array when the original array is modified, it still iteration as expect.



Answered By - Ninh Le
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing