Programming Ruby 3.2 (5th Edition): B4.0 pages 558-559, discrepancy in code

@noelrappin

pages 558-559, in CSV

% ruby -w sl_csv/sample_0.rb
sl_csv/sample_0.rb:9:in `chdir': No such file or directory @ dir_s_chdir - code/sl_csv (Errno::ENOENT)
	from sl_csv/sample_0.rb:9:in `<main>'

The downloaded code has a chdir before require which does not appear in the book :

Dir.chdir("code/sl_csv")
require "csv"
CSV.foreach("csvfile.csv") do |row|
...

Until now all code had to be run with code as the current directory, so code in "code/sl_csv" is wrong. The statement must be :

Dir.chdir("sl_csv")

or without Dir.chdir :

CSV.foreach("sl_csv/csvfile.csv") do |row|

Same problem with sample_1.rb and sample_3.rb.

This comes out of some internal logistic changes between versions.

I think the most robust fix is to use #{__dir__} in the code