Ruby Question (using a Mac)

Getting a error I don't get on a PC or Linux

Page 1 of 1

2 Replies - 925 Views - Last Post: 15 March 2010 - 06:14 PM Rate Topic: -----

#1 Merddin  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 37
  • Joined: 19-February 10

Ruby Question (using a Mac)

Posted 15 March 2010 - 04:35 PM

Hello,

I was recently helping someone learn Ruby. They are using a Mac (which I am not at all familar with). We were going through the Pick-Axe book (1.9) and typing in code (which I know works on PC and Linux) and received some errors. The code is:

require 'csv'
require_relative 'book_in_stock'

class CsvReader
    def initialize
       @books_in_stock = []
    end
    def read_in_csv_data(csv_file_name)
       CSV.foreach(csv_file_name, headers: true) do |row|
          @books_in_stock << BookInStock.new(row["ISBN"], row["Amount"])
       end
    end
    def total_value_in_stock
       sum = 0.0
       @books_in_stock.each {|book| sum += book.price}
       sum
    end
end



We received two errors. The first was pointing at
CSV.foreach(csv_file_name, headers: true) do |row|

and stated unexpected : expecting )

The second was in regards to the final end statement and said
unexpected kend expecting #end

All, I can figure is that it is either because it is a Mac or because we weren't running Ruby 1.9 (we couldn't find it to download for the Mac). Although the book is good at indicating which code snippets require 1.9 and this one was not marked.

Thanks everyone for your help

This post has been edited by Merddin: 15 March 2010 - 04:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Ruby Question (using a Mac)

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Ruby Question (using a Mac)

Posted 15 March 2010 - 06:05 PM

headers: true


This short form is unique to Ruby 1.9

In 1.8, you would need to use the longer, but equivalent:
:headers => true


As for upgrading your installation on Mac, you could grab the source (direct link) and build it from there. (There might be a prebuilt binary, but I'm not sure)
Was This Post Helpful? 2
  • +
  • -

#3 Merddin  Icon User is offline

  • New D.I.C Head

Reputation: 12
  • View blog
  • Posts: 37
  • Joined: 19-February 10

Re: Ruby Question (using a Mac)

Posted 15 March 2010 - 06:14 PM

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1