Akee75
Akee75

Reputation: 11

ruby sinatra: "irb require" issue

I'm discovering Sinatra and have started playing with it. All was just fantastic until the tutorial asks me to require a file in irb, so that the Song class is available to IRB:

irb> require './song'

I followed the tutorial to the letter, however, I keep getting the same error message below:

MacBook-Pro-de-doguria:views arnaud$ ls
about.slim  home.slim   not_found.slim  styles.scss
contact.slim    layout.slim song.rb
MacBook-Pro-de-doguria:views arnaud$ irb
irb(main):001:0> require './song'
/Users/arnaud/Google Drive/Code/codebasics/sinatra2/views/song.rb:1: warning: encountered \r in middle of line, treated as a mere space
SyntaxError: /Users/arnaud/Google Drive/Code/codebasics/sinatra2/views/song.rb:1: syntax error, unexpected tIDENTIFIER, expecting end-of-input
DataMapper.s...rations'
                     ^
from /usr/local/Cellar/ruby/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /usr/local/Cellar/ruby/2.2.3/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from (irb):1

My file song.rb was copy pasted from the tutorial, and can be found below:

require 'dm-core'
require 'dm-migrations'
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/development.db")
class Song
  include DataMapper::Resource
  property :id, Serial
  property :title, String
  property :lyrics, Text
  property :length, Integer
  property :released_on, Date
end
DataMapper.finalize

As you can guess, I'm fairly new to programming, apologies in advance if it is something very basic that I overlooked. Cheers.

Upvotes: 1

Views: 204

Answers (2)

Akee75
Akee75

Reputation: 11

Thank you everyone for your help and suggestions, much appreciated. Issue kind of sorted now, as I simply used another file name (ie. songs.rb instead of song.rb), which worked. As explained above, my first attempt was copy pasted, but I subsequently wrote the original code in a new file. Still don't know what happened, but that's how one learns I guess...

Upvotes: 0

Felix
Felix

Reputation: 4716

It seems that you have a problem with your file encoding.

Ruby shouts out a SyntaxError on line 1 of song.rb (read the error message carefully - it does not look as if line 1 would be what you want to be line 1), and complains about a stray \r in your but your song.rb file.

I assume you copy-pasted the code from somewhere into somewhere and these somewhere-applications did not behave cool.

Either you take on the journey to learn about file-encodings and conversions, or you find an option in your editor to save the file in the given encoding, or you re-copy the code into a new file and hope it just works(tm).

Upvotes: 1

Related Questions