How to reference a file from inside of a gem - ruby

What's the correct way to find the absolute path to a file from within a gem? In other words, let's say I'm in gem_install_path/mygem/lib/mygem.rb, and I want the path to gem_install_path/mygem/foo/. What's the correct way to get that?

It is not clear which path you want, but probably one of the following will give you what you want:
Gem.loaded_specs["mygem"].base_dir
Gem.loaded_specs["mygem"].full_gem_path
Gem.loaded_specs["mygem"].gem_dir
Gem.loaded_specs["mygem"].gems_dir

Related

How can I re-require in ruby?

How can I re-require in ruby?
I know about load, but with that, I need to specify the path. I want ruby to do that path lookup.
load does not need a full path, it needs a complete filename including an extension.

Store directory path in variable?

I want to store a path like c:\abhor\test in a variable but it is treated as \a,\t. I don't want to use c:\\abhor\\test. Please provide some solution to this problem?

In Ruby, how do I specify a file in another directory as an input?

This probably has a simple answer, but I'm working on a test suite that requires an input file that is in a different folder. I'd like to use a relative path, like this:
#graph = Graph.new('../lib/test_input.txt')
But Ruby doesn't like that. What's the best way to use a relative file path like that?
Thanks
If you mean relative to the current file, you'll probably want something like:
#graph = Graph.new(File.expand_path(__FILE__, "../lib/test_input.txt"))
If you mean relative to the current directory, you'll probably want something like:
#graph = Graph.new(File.expand_path(Dir.pwd, "../lib/test_input.txt"))
bonus link!

How to test file creation with RSpec?

I have a simple FileCreator Ruby class that has 1 method create which creates a blank txt file on my desktop. Using RSpec, how would I test this create method to make sure
that the file was created, without having to create the file? Would I use RSpec::Mocks? Can someone please point me in the right directory? Thanks!
After calling file_creator.create(100) you could search the folder for all File*.txt files and make sure the count matches. (Make sure to have your spec remove the test files after completion).
Dir.glob(File.join(File.expand_path("~/Desktop"), "File*.txt")).length.should == 100
Using Mocks: You could do something like this to verify that the File.open method is actually being called (to test that the files actually get created, though, you may want to consider actually creating the files like the first half of my answer).
File.should_receive(:open).exactly(100).times
You could also try using something like FakeFS which mocks the actual file system.
The simplest way to do it is as below:
FileCreator.count.should eq 100

I can't figure out the require in ruby

I'm new to Ruby
MakModule.rb
module Display
class Multiply
def Multiply.mul(first, second)
return first * second
end
end
end
MakRequire1.rb
require "Display"
puts Multiply.mul 5,6
MakRequire2.rb
require "MakModule.rb"
puts Multiply.mul 5,6
both file give me the error below
ruby: No such file or directory -- makRequire (LoadError)
How should I correct my code?
It is simply impossible that the code you posted here generates that error message. The error message says that you tried to require a file named makRequire, but that filename doesn't appear anywhere in the code you posted.
Without the actual code that is generating the actual error, it is impossible to answer your question. However, here are a few general tips:
Whenever the computer tells you that it cannot find something, in 99% of the cases, the problem is that the thing the computer tells it couldn't find isn't actually there.
So, in this case, the computer tells you that it cannot find a file named makRequire.rb, and the most likely explanation for that is that makRequire.rb doesn't actually exist. So, the first thing you need to check is: does makRequire.rb (note the capitalization and the file extension) actually exist? Because if it doesn't exist, then the reason why the computer cannot find it, should be rather obvious.
In 99% of the rest of the cases, the problem is that the thing the computer is looking for does exist, but the computer is looking in the wrong place. So, after you have verified that makRequire.rb actually does exist, you need to make sure that the directory the file is in, is in Ruby's $LOAD_PATH, and if it isn't, you need to add that directory to Ruby's $LOAD_PATH. Alternatively, if you want to require the file relative to the path of the file that is doing the requiring, you need to use require_relative instead of require.
The third thing to check for, is whether the user who own the ruby process has sufficient privileges to access the file makRequire.rb, the directory it is in and all of its parent directories.
Try this,
require File.join(File.dirname(__FILE__),'MarkModule')
Try require './MakModule', because the . is the current directory.
require 'MakModule'
You can require a file that is in the same directory. To use a module you would typically include the module inside a class definition. So you would never require Display, you would require the file that contains Display (without the .rb extension, usually).

Resources