Ruby: File Path using $:.unshift(File.join('DIR1', 'DIR2')) - ruby

im trying to figure out what exactly these methods are doing.
$:.unshift(File.join(APP_ROOT, 'lib'))
I know its for file paths but if this piece of code were to broken down into segments how would you describe each one?
So far I under stand the File.join part, which takes two arguments (the APP_ROOT variable, and the 'lib' directory.) It then unshifts something?
Thanks in advance.

$: is Ruby shorthand to the load path array, i.e. an array full of the paths that Ruby uses to look up external files when asked to require one (try running it in IRB).
In Ruby, .unshift is a method that takes the given path (in this case, whatever File.join(APP_ROOT, 'lib') resolves to), and prepends it to the beginning of the load path array.
This way Ruby will know to check the APP_ROOT/lib path the next time you do a require 'myfile' line elsewhere in the app.

Related

How to check if a directory contains a .pdf file in Ruby?

A lot of people send me their invoices and they are almost always .pdf files. I have a limited attention span and so have written some ruby code to save me having to look at them. It would help if my bit of Ruby code would look into a folder and, if there was an invoice there, return the name of the file and its path. The thing is that the people who send me their invoices are very annoying and name their files in the worst way. For example, one of them has given me an invoice called "Invoice_Cost_Centre_6454537.pdf". I can't be expected to remember that sort of name. However they are always (almost) .pdfs.
I was taking to Ruby about it and she said this,
File.exist?('test\Invoice_Cost_Centre_61356.pdf')
#=> true
File.exists?('test\*.pdf')
#=> false
I have had a look at the docs for the File class and thought that File.fnmatch?(pattern, path) might be a solution but I couldn't get it to work.
I would appreciate any help. I'm fairly new to Ruby so please keep your sentences short.
File.exists? checks if a file with that path exists verbatim.
File.fnmatch? checks if a path matches a given pattern, but doesn't actually look up that path.
Instead you could Dir['test/*.pdf'].empty?

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.

Zip command in ruby block does not work without binding.pry

I have a block of code that executes a zip command from another class:
def zip_up_contents path name
Zipper.new path name
end
The problem is that it zips blank copies of all the files passed to it. But when I put a binding before the zip command like so:
def zip_up_contents path name
binding.pry
Zipper.new path name
binding.pry
end
it zips the files successfully. I know this by checking the resulting file's byte size from within pry on the second binding point with and without the first binding present. Without the binding, the zip archive's byte size is half what it should be, and with the binding it's the size I would expect.
The "Zipper" class simply calls the system zip with backticks. I don't think that's the problem because I've used that class without trouble in other contexts. The zip utility is Zip 3.0 on Ubuntu 10.04.
I have no idea why the presence of the binding makes a difference. If anyone has encountered anything similar, or has thoughts about how to better debug the issue, I'd appreciate hearing about it.
EDIT: For anyone encountering anything similar, I resolved this by calling fsync on the files prior to zipping them: http://www.ruby-doc.org/core-2.0.0/IO.html#method-i-fsync
I had this problem with some Rspec tests last week -- turned out to be a race condition. Is there any chance that the file hasn't been saved when you pass it to be zipped? I mean, maybe the binding.pry is just giving it a chance to catch up.

Should I use load or require in IRB?

I had the impression that I should use require 'some_path' to get a file or a library I want to use, but I couldn't get it to work, and load 'some_path' worked.
When are the different times when I should use these?
Also, is it possible to import a directory with the load or require commmands? I am trying to use a whole directory, but so far I could only get one file at a time with load.
You can use Dir to list all the files ending with .rb and require/load them
Dir["/path/to/dir/*.rb"].each { |file| load_your_file_here(file) }
I recommend requiring file and then including the module that file loads... If you are not using module or class inside your file than maybe you should reconsider your structure.
load might have some unintended consequences and it's not performant.
Once you call require for a file further calls of require will no longer require it again(i.e. will have no effect), while load will reload it every time you call it. As far as I know there is no way to load a whole directory.

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