BernardK

BernardK

Programming Ruby 3.2 (5th Edition): B1.0 page 205, Only then do we create

@noelrappin

page 205, paragraph after the code, line 2 :

then blocks at Ractor.receive. Only then do we create the reading ractor

Because of “Only then” I thought that creating the second Ractor could only take place when the first is waiting on receive. But the trace shows that both Ractors are created, then the blocks are scheduled.

On page 204 (par. 5, code for one, line 1) I liked “Moving down the file”, for a possible replacement of “Only then”.

Having said that, it’s impressive to see the reader sending several times without waiting for a take.

def displayWord(p_word)
    p_word.nil? ? "^nil^" : "|#{p_word}|"
end
    
puts "*** (main) about to create counter"
counter = Ractor.new(name: "counter") do
  puts ">>> ccccc in counter block"
  result = Hash.new(0)
  while (word = Ractor.receive)
    puts "ccccc in counter received word=#{displayWord(word)}"
    result[word] += 1
    puts "ccccc looping"
  end
  puts "ccccc in counter while ended word=#{displayWord(word)}"
  result
end

puts "*** (main) about to create reader"
Ractor.new(counter, name: "reader") do |counter|
  puts ">>> rrrrr in reader block"
  File.foreach("testfile") do |line|
    puts "rrrrr line=#{line}"
    line.scan(/\w+/) do |word|
      puts "rrrrr about to send word=#{displayWord(word)}"
      counter.send(word.downcase)
      puts "rrrrr after send"
    end
  end
  puts "rrrrr about to send nil"
  counter.send(nil)
end

puts "*** (main) about to take counter"
counts = counter.take
counts.keys.sort.each { |k| print "#{k}:#{counts[k]} " }
puts
% ruby -w ractor_word_count_flipped.rb 
*** (main) about to create counter
<internal ... experimental ...
*** (main) about to create reader
*** (main) about to take counter
>>> ccccc in counter block
>>> rrrrr in reader block
rrrrr line=This is line one
rrrrr about to send word=|This|
rrrrr after send
rrrrr about to send word=|is|
rrrrr after send
rrrrr about to send word=|line|
ccccc in counter received word=|this|
...

First Post!

noelrappin

noelrappin

Author of Modern Front-End Development for Rails

Thanks!

Where Next?

Popular Pragmatic Bookshelf topics Top

brianokken
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
johnp
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
edruder
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
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
conradwt
First, the code resources: Page 237: rumbl_umbrella/apps/rumbl/mix.exs Note: That this file is missing. Page 238: rumbl_umbrella/app...
New
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
New
New

Other popular topics Top

New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
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
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
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
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
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New

Sub Categories: