Hello, I'm learning Ruby (using the Pickaxe book) and I've encountered a little confusion with a block operation that goes a little like this:
class CsvReader
def initialize
@book_in_stock = []
end
def read_in_csv_data(csv_file_name)
CSV.foreach(csv_file_name, headers: true) do |row| # 1.
@book_in_stock << BookinStock.new(row["ISBN"], row["Amount"]) # 2.
end
end
end
My confusion exists on the commented "#1" and "#2", I don't understand how we get "123" and "456" from a csv_file_name of "test.csv" containing the data:
"ISBN","Amount"
"123","456"
How is it that row["ISBN"] knows that the next row corresponds to it? Do all blocks behave this way? What are they really called? Could someone explain them a little better?
Thanks.