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.
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!
Popular Pragmatic Bookshelf topics
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
Many tasks_proj/tests directories exist in chapters 2, 3, 5 that have tests that use the custom markers smoke and get, which are not decl...
New
Hi Brian,
Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New
Title: Web Development with Clojure, Third Edition, pg 116
Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New
your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
New
Hi Travis! Thank you for the cool book! :slight_smile:
I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
@noelrappin
Running the webpack dev server, I receive the following warning:
ERROR in tsconfig.json
TS18003: No inputs were found in c...
New
Hi,
I completed chapter 6 but am getting the following error when running:
thread 'main' panicked at 'Failed to load texture: IoError(O...
New
Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
Other popular topics
What chair do you have while working… and why?
Is there a ‘best’ type of chair or working position for developers?
New
Think Again 50% Off Sale »
The theme of this sale is new perspectives on familiar topics.
Enter coupon code ThinkAgain2021 at checkout t...
New
Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
Hi folks,
I don’t know if I saw this here but, here’s a new programming language, called Roc
Reminds me a bit of Elm and thus Haskell. ...
New
This is going to be a long an frequently posted thread.
While talking to a friend of mine who has taken data structure and algorithm cou...
New
New
Will Swifties’ war on AI fakes spark a deepfake porn reckoning?
New
Jan | Rethink the Computer.
Jan turns your computer into an AI machine by running LLMs locally on your computer. It’s a privacy-focus, l...
New
Curious what kind of results others are getting, I think actually prefer the 7B model to the 32B model, not only is it faster but the qua...
New
Ask Me Anything with
Mark Volkmann
@mvolkmann
On February 24 and 25, we are giving you a chance to ask questions of PragProg author M...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /rails
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /zig
- /deepseek
- /scala
- /sublime-text
- /textmate
- /lisp
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /django
- /deno
- /revery
- /ubuntu
- /nodejs
- /manjaro
- /spring
- /diversity
- /lua
- /julia
- /c
- /slackware







