
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

Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’.
In particula...
New

I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here.
...
New

Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New

Hi! I know not the intentions behind this narrative when called, on page XI:
mount() |> handle_event() |> render()
but the correc...
New

I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
New

Title: Web Development with Clojure, Third Edition, vB17.0 (p9)
The create table guestbook syntax suggested doesn’t seem to be accepted ...
New

@noelrappin
Running the webpack dev server, I receive the following warning:
ERROR in tsconfig.json
TS18003: No inputs were found in c...
New

In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New

Modern Front-End Development for Rails - application does not start after run bin/setup (page xviii)
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New

root_layout: {PentoWeb.LayoutView, :root},
This results in the following following error:
no “root” html template defined for PentoWeb...
New
Other popular topics

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New

I’m thinking of buying a monitor that I can rotate to use as a vertical monitor?
Also, I want to know if someone is using it for program...
New

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
https://on...
New

Just done a fresh install of macOS Big Sur and on installing Erlang I am getting:
asdf install erlang 23.1.2
Configure failed.
checking ...
New

Seems like a lot of people caught it - just wondered whether any of you did?
As far as I know I didn’t, but it wouldn’t surprise me if I...
New

API 4
Path:
/user/following/
Method:
GET
Description:
Returns the list of all names of people whom the user follows
Response
[
{ ...
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

Author Spotlight
James Stanier
@jstanier
James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New

I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /scala
- /html
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /react-native
- /textmate
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /spring
- /django
- /manjaro
- /diversity
- /nodejs
- /lua
- /julia
- /slackware
- /c
- /neovim