Programming Ruby 3.2 (5th Edition): page 189 page_to_fetch variable question

There is an example of using Threads on page 188:

require "net/http"

pages = %w[www.rubycentral.org www.pragprog.com www.google.com]

threads = pages.map do |page_to_fetch|
  Thread.new(page_to_fetch) do |url|
    puts "inside thread id:#{url.object_id}, value:#{url}"
    http = Net::HTTP.new(url, 80)
    print "Fetching: #{url}\n"
    response = http.get("/")
  end
end
threads.each { |thread| thread.join }
print "We're done here!\n"

Then on page 189 5th paragraph, it says:

The first thread gets started, and page_to_fetch is set to “www.rubycentral.org”. The meantime, the loop creating the threads is still running. The second time around, page_to_fetch gets set to “pgragprog.com”. If the first thread hasn’t yet finished using the page_to_fetch variable, it’ll suddenly start using this new value.

As I understand the last sentence here is wrong. :thinking: page_to_fetch is going to point to the string objects during the loop so no thread will point to the same string.

See below code with additional put statements and the output. Note that object ID is always different:

require "net/http"

pages = %w[www.rubycentral.org www.pragprog.com www.google.com]

threads = pages.map do |page_to_fetch|
  puts "outside thread page_to_fetch id:#{page_to_fetch.object_id}, value:#{page_to_fetch}"
  Thread.new(page_to_fetch) do |url|
    puts "inside thread url id:#{url.object_id}, value:#{url}"
    http = Net::HTTP.new(url, 80)
    print "Fetching: #{url}\n"
    response = http.get("/")
    print "Got #{url}:  #{response.message}\n"
  end
end
threads.each { |thread| thread.join }
print "We're done here!\n"

output:

outside thread page_to_fetch id:60, value:www.rubycentral.org
outside thread page_to_fetch id:80, value:www.pragprog.com
outside thread page_to_fetch id:100, value:www.google.com
inside thread url id:60, value:www.rubycentral.org
Fetching: www.rubycentral.org
inside thread url id:100, value:www.google.com
Fetching: www.google.com
inside thread url id:80, value:www.pragprog.com
Fetching: www.pragprog.com
Got www.google.com:  OK
Got www.rubycentral.org:  Found
Got www.pragprog.com:  Moved Permanently
We're done here!