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

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
HarryDeveloper
Hi @venkats, It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New
jeremyhuiskamp
Title: Web Development with Clojure, Third Edition, vB17.0 (p9) The create table guestbook syntax suggested doesn’t seem to be accepted ...
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
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
adamwoolhether
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
AufHe
I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113. Iteration A1 - Seeing the list of prod...
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New
dachristenson
I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
AstonJ
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: Perhaps if there’s enough peop...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
Exadra37
I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1139 25652 757
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
New
New

Sub Categories: