0

I'm experiencing a strange issue with a ruby function I wrote to remove unwanted items from a hash created by parsing a JSON string. The function will return what I am expecting when I have pry statements included in the function as seen at the bottom of this post. When I remove the pry statements the function returns something else. I'm not really sure why that would be the case and figured I'd ask here while I try to figure out an alternative solution to my problem.

Below, slides is a string of space separated integers such that slides.split(' ') returns an array of integers. Slide groups is a parsed JSON hash. If requested I can provide a file with the JSON hash, but it was getting frustrating to try and enter it here. I don't think it is necessary to see the hash since it seems the issue lies with pry and the effect it is having on the function.

def selected_slides_and_groups
  selected_slide_ids = slides.split(' ')
  slide_groups = master_presentation.slide_groups

  slide_groups.each do |slide_group|
    delete_slides_from_group(slide_group, selected_slide_ids)
  end
end

def delete_slides_from_group(slide_group, selected_slide_ids)
  binding.pry
  slide_group[:content].delete_if do |item|
    if item[:type] == 'group'
      delete_slides_from_group(item, selected_slide_ids)
    elsif selected_slide_ids.include? item[:id]
      next
    else
      true
    end
  end
  binding.pry
end
E Newmy
  • 185
  • 1
  • 1
  • 11
  • That was exactly it. Thank you very much for your help! Edit: trying to accept your answer, but since it is a comment I'm not sure I can. I'll check back after I return from some errands and if I can I'll accept your answer. Thanks again! – E Newmy Oct 10 '18 at 17:27
  • 1
    Since my comment is a valid answer, I added it as one. :) – Marek Lipka Oct 10 '18 at 17:40

1 Answers1

1

Methods in Ruby return last evaluated value (unless specified otherwise) and binding.pry call returns nil.

Marek Lipka
  • 50,622
  • 7
  • 87
  • 91