How to use remote files for classes/etc with Ruby? - ruby

I'm test SciTe editor, and Gosu for ruby game development. I decided to make a class to control my sprites. The class was written in a separate .rb file, in the same folder as the main .rb file. However, I can't use Sprite.new in the main file. How can I do that?

require 'sprite'
(Or whatever it's called.)
As Frederick notes, if you're using Ruby 1.9, the current directory isn't automatically part of the load path (not sure if I agree with the decision). You can add it on the command line like this:
ruby -I. main.rb

require File.join(File.dirname(__FILE__), '', 'MyFile')
I don't know why. But just require simply doesn't always work on different OS. But the above code always works for me.

Related

What is a good way to see output from my ruby gem?

I want to know a good way to run code in my ruby gem (and not just through tests).
That means I want to run ruby lib/{gemname}.rb on the terminal and be able to see some output
So I have this line in my base file:
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
and then I load a file called debugger.rb by requiring it at the bottom of the base file. This file then gives me the output I need.
This works but this kind of clutters my code and I don't want to accidentally commit it and watch it break in production.
So what's a good way of doing this?
My approach for developing and debugging gems has two parts:
1) Always use require_relative to include needed "internal" gem related files. This allows the gem to be loaded up normally when installed as a gem and also in my development environment (ignoring any versions of the gem that may already be installed.)
2) Then use the following snippet of code (usually) at the bottom of the main file, to activate debugging features when the base my_gem.rb file is run explicitly on the command line.
if __FILE__ == $0
#debugging code goes here!
end
With this strategy there's no need to worry about yanking debug code before releasing the gem.

ruby require './blahblah.rb' vs require File.expand_path('../blahblah', __FILE__)

What is the difference between
require 'blahblahlblah.rb'
vs
require './blahblah.rb'
vs
require File.expand_path('../blahblah', __FILE__)
I see both of them being used. Wondering what's better, and under what circumstance is one better than the other.
Thanks!
require blaba.rb is searching to your default gem path to load the file, which depends on the ruby version you are using. For example RVM will search in $HOME/.rvm/rubies/... while a system wide ruby will search in the distribution's default path. Note that this is where gems are located, but you could manually add a library say mylibrary.rb in the same path and use it in any of your programs. However, that's an awful thing to do, it's a much cleaner procedure to create gems and install them in your system.
require ./blabla.rb loads a file that is sitting in your working directory. You could add the full path like require /home/username/library/myproject/models/sample.rb. It will work just about the same. In the UNIX-like world the ./ sign means current directory. This solution is often used in irb to load say a rails Model i.e. users.rb into irb or pry and work with it. To give you an example in a shell environment (if you are familiar with UNIX shells, you'll figure it out):
GreyJewel ~ » ls myports.txt
myports.txt
GreyJewel ~ » ls ./myports.txt
./myports.txt
The third solution require File.expand_path('../sample.rb', __FILE__) is used in programs, because it explicitly creates a full path using as an anchor the directory which the file holding the line sits, which is a much more secure approach compared to require ./sample.rb. Note that when you load a ruby file, you can omit the file extension .rb.
Hope this clarifies a bit the situation.

Making an executable from ruby files using Ocra, LoadError?

I'm trying to make an executable from a couple ruby files on Windows, so I installed Ocra. I thought I understood the process of how Ocra works, but can't seem to get the executable working correctly. The problem I am having arises with "requiring" other ruby files.
The ruby program by itself compiles correctly and functions how I want it to, and Ocra seems to create a working executable, however, when I attempt to run the executable, I get the following error:
*/custom_require.rb:36:in 'require': cannot load such file -- MainMenuDialog.rb (LoadError)*
My main program is called 'JobManager.rb' and it is creating a new MainMenuDialog object, therefore I include MainMenuDialog.rb in the top of the file as such:
$: << File.expand_path(File.dirname(__FILE__) + "/../lib")
# Other requires here
require("MainMenuDialog.rb")
Again, the program compiles and runs perfectly fine by itself (including the require statements), and when I run the command ocra JobManager.rb it successfully runs the programs, checks for dependencies, and creates the executable. I just can't run the executable because of the "LoadError" described above.
Any thoughts on what I'm doing wrong? Thanks in advance!!!
With ruby >= 1.9 you may try require_relative.
Explanation: ocra stores all files in its own subdirectories.
If you manipulate the load pathes ($:) you can't be sure, what ocra uses during execution.
This question is old and answered, but I wanted to include this little nugget I found in Ocra documentation, because the correct answer here did not resolve my issue:
OCRA does not set up the include path. Use $:.unshift
File.dirname($0) at the start of your script if you need to 'require'
additional source files from the same directory as your main script.

How to I load a gem from source?

I have git cloned a repo from Github, now I want to experiment with it, as in I want to poke around the code and mess with it. I've created a file test.rb that should load this gem, but I want to load my locally checked out version, what's the right way to do this?
Right now I'm just using a bunch of "require_relative 'the_gem_name/lib/file'", which feels wrong.
When you require 'foo' Ruby checks all the directories in the load path for a file foo.rb and loads the first one it finds. If no file named foo.rb is found, and you’re not using Rubygems, a LoadError is raised.
If you are using Rubygems (which is likely given that it is included in Ruby 1.9+), then instead of immediately raising a LoadError all the installed Gems are searched to see if one contains a file foo.rb. If such a Gem is found, then it is added to the load path and the file is loaded.
You can manipulate the load path yourself if you want to ensure a particular version of a library is used. Normally this isn’t something that’s recommended, but this is the kind of situation that you’d want to do it.
There are two ways of adding directories to the load path. First you can do it in the actual code, using the $LOAD_PATH (or $:) global variable:
$LOAD_PATH.unshift '/path/to/the/gems/lib/'
require 'the_gem'
Note that you normally want to add the lib dir of the gem, not the top level dir of the gem (actually this can vary depending on the actual Gem, and it’s possible to need to add more than one dir, but lib is the norm).
The other way is to use the -I command line switch to the ruby executable:
$ ruby -I/path/to/the/gems/lib/ test.rb
This way might be a bit cleaner, as normally you don’t want to be messing with the load path from inside your code, but if you’re just testing the library it probably doesn’t matter much.
Following apneadiving's suggestion in the comments, I created a Gemfile and added this line
source "http://rubygems.org"
gem 'gem_name', path: '~/path/to/gem/source/folder'
Then bundle install, and bundle exec ruby test.rb and it worked.

ruby require not working

I'm new to ruby, but I'm working on my first ruby program. It currently has two files, one is a library of functions (xgync.rb stored in lib) the other is the executable xgync stored in 'bin'. (Project visible here https://bitbucket.org/jeffreycwitt/xgync/src) I've also created a symlink to my /usr/local/bin/xgync so that I can write the command xgync {arguments} from anywhere in the terminal.
The problem seems to be that bin/xgync depends on the library lib/xgync.rb. I've written this dependency in bin/xgync as follows:
$:.unshift(File.dirname(__FILE__) + '/../lib')
require "xgync"
However, i keep getting the following error:
/Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- xgync (LoadError)
from /Users/JCWitt/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /usr/local/bin/xgync:4:in `<main>'
can you see anything wrong with what I've written? Could the symlink be somehow messing things up?
Thanks for your help :)
When using ruby 1.9.x you don't usually alter the path with the $:.unshift when requiring other files in your project.
Instead the best practice is to use require_relative instead.
require_relative '../lib/xgync.rb'
require_relative requires files relative to the file you are currently editing.
But the error you experience appears, because you require a file, which does not exist:
bin/xgync
lib/xgync.rb
These are the files in your project according to your question, and the code-excerpt is from bin/xgync you extended the path to look for files in lib/ but you try to require 'xgync' which is a file, that is not present in lib/, so if you wanted to use this method (instead of require_relative you would have to use require 'xgync.rb'.

Resources