How can I re-require in ruby? - 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.

Related

Get the source code of a ruby file in the form of a string/text

Is there any simple way to get all the source code of a ruby file in the form of a string? I'm looking for something that would behave like inspect.sourcelines(codeObj) in python.
I'm surprised this hasn't been asked before... But I couldn't find the question on SO anywhere...
myCode = File.read(__FILE__)
That should do the trick.
__FILE__ is a special variable which contains the full file path of the currently executing file. Depending how you script is being executed you might be better off using $0 instead. But this really depends on what you really want.

How to reference a file from inside of a gem

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

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

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.

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).

Save WWW::Mechanize::File to disk using FileUtils

Using Mechanize with Ruby I get a certain file using agent.get('http://example.com/foo.torrent'), with FileUtils or otherwise, how do I save this file to my hard drive (for instance, in a directory wherefrom the script is running)?
P.S. class => WWW::Mechanize::File
Well, WWW::Mechanize::File has a save_as instance method, so I suppose something like this might work:
agent.get('http://example.com/foo.torrent').save_as 'a_file_name'
Please note that the Mechanize::File class is not the most appropriate for large files. In those cases, one should use the Mechanize::Download class instead, as it downloads the content in small chunks to disk. The file will be downloaded to where the script is running (although you can specify a different path as well). You need to set the default parser first, create a new one or modify an existing parser. Then, save it to the desired path:
agent.pluggable_parser.default = Mechanize::Download
agent.get( "http://example.com/foo.torrent}").save("path/to/a_file_name")
Check here and here for more details. Also, there's a similar question here in Stackoverflow.

Resources