I'm using the Learn Game Programming with Ruby book and I'm trying to just execute the sample code.
I get the following error, using the sample code.
❯ ruby WhackARuby/WhackARuby_1/whack_a_ruby.rb code
/Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosu-0.10.4/lib/gosu/patches.rb:40:in `initialize': Cannot open file ruby.png (RuntimeError)
from /Users/noahclark/.rvm/gems/ruby-2.2.1/gems/gosuu0.10.4/lib/gosu/patches.rb:40:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:15:in `initialize'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `new'
from WhackARuby/WhackARuby_1/whack_a_ruby.rb:70:in `<main>'
The sample code looks like this:
require 'gosu'
class WhackARuby < Gosu::Window
def initialize
super(800, 600)
self.caption = 'Whack the Ruby!'
@image = Gosu::Image.new('ruby.png')
end
end
Any thoughts on what could be going on here? I've tried changing the offending line to @image = Gosu::Image.new('./ruby.png')
for example and that didn't help.
I doubt this is the cause, but my ruby version is ruby 2.2.1p85 (2015-02-26 revision 49769) [x86_64-darwin14]
Invariably the problem is because the file doesn't exist where you think it is.
There are many ways to reference a file. The File documentation has expand_path
, realpath
, absolute_path
, all of which make it easy to reference a file based on an absolute or relative path, and relative to the currently running file, application or a particular directory. How to use them is covered in their examples.
It's important to make sure you know what directory the code considers it's current-working-directory, and/or where the file is. The first is important when using a relative path, and the second is if you don't want to care about your current path and know that the file ALWAYS exists in a certain place.
And, then there's also the case when the file's name is different than what you think it is, or it doesn't even exist.