sinaru

sinaru

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!

Where Next?

Popular Pragmatic Bookshelf topics Top

jimmykiang
This test is broken right out of the box… — FAIL: TestAgent (7.82s) agent_test.go:77: Error Trace: agent_test.go:77 agent_test.go:...
New
New
adamwoolhether
When trying to generate the protobuf .go file, I receive this error: Unknown flag: --go_opt libprotoc 3.12.3 MacOS 11.3.1 Googling ...
New
brunogirin
When installing Cards as an editable package, I get the following error: ERROR: File “setup.py” not found. Directory cannot be installe...
New
New
jwandekoken
Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below: d...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
a.zampa
@mfazio23 I’m following the indications of the book and arriver ad chapter 10, but the app cannot be compiled due to an error in the Bas...
New
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New

Other popular topics Top

Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
New
PragmaticBookshelf
Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight Jamis Buck @jamis This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New
PragmaticBookshelf
Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
New

Sub Categories: