mercredi 11 juillet 2018

How come my duplicate also effects the original?

I'm trying to add copies to an array with some modification:

  site.pages.dup.each do | page |
    new_page = page.dup
    data = new_page.data
    data['permalink'] = File.join('/app', page.url)
    data['layout'] = 'app'
    site.pages << new_page
  end

  site.pages.each do | page |
    puts page.data
  end 

When I output page.data, the permalinks are different, but data['layout'] is the same for all items in the array. I also tried data = new_page.data.dup and writing out new_page.data in each instance.

Aucun commentaire:

Enregistrer un commentaire