0

I' trying to add duplicate items to an array while looping through it:

Adding it to the end causes an infitie loop:

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

so does adding it to the beginning.

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

1 Answer 1

1

I would create all the duplicates first and then add them to the array in a second step.

new_pages = site.pages.map do |page|
  new_page = page.dup
  new_page.data['permalink'] = File.join('/app', page.url)
end
site.pages += new_pages
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.