Show full path name of the ruby file when it get loaded
The best way I can think of is to patch the Kernel
module. According to the docs Kernel::load
searches $:
for the filename. We can do the same thing and print the path if we find it.
module Kernel
def load_and_print(string)
$:.each do |p|
if File.exists? File.join(p, string)
puts File.join(p, string)
break
end
end
load_original(string)
end
alias_method :load_original, :load
alias_method :load, :load_and_print
end
We use alias_method
to store the original load
method, which we call at the end of ours.
Getting the full path for a required file
You can look for loaded files in $LOAD_PATH
aka $:
variable as described in Show full path name of the ruby file when it get loaded. And you can list required files by querying $LOADED_FEATURES
aka $"
require '/home/ashish/samples/required_test.rb'
$".each do |path|
puts path
end
More info on require - http://www.ruby-doc.org/core-1.9.3/Kernel.html#method-i-require
How to get the path of loaded files
Read the last element of the array $LOADED_FEATURES
right after a file load succeeds.
...
require 'set'
$LOADED_FEATURES.last # => gives the path for `set` if it was loaded properly
...
require 'abc/pqr'
$LOADED_FEATURES.last # => gives the path for `abd/pqr` if it was loaded properly
...
If you need to do it later, then you need to search the appropriate path from the $LOADED_FEATURES
using some kind of string match.
Ruby, getting path from path+filename
Use the Ruby File.dirname
method.
File.dirname("C:/Test/blah.txt")
# => "C:/Test"
How can I get the full path of my selected file in Ruby on Rails?
You can't get path to file from form in modern browsers. You can work only with tempfile Should be available as params[ :uploaded_file ][ :tempfile ]
Edit try
file = Spreadshet::Excel.new('params[ :uploaded_file ][ :filename ]', 'w+')
file.write( params[ :uploaded_file ][ :tempfile ].read )
How to get the current working directory's absolute path in Ruby?
Dir.pwd
is the current working directory
http://ruby-doc.org/core/Dir.html#method-c-pwd
Ruby: How to get the correct full path of a source file after a chdir?
__FILE__
builtin is an instance of String
class:
puts __FILE__.class
# ⇒ String
That means you should not expect any voodoo magic from it. It stores the relative path, this file was loaded at.
ruby C:\TEMP\test.rb # ⇒ __FILE__ == 'C:\TEMP\test.rb'
cd C:\TEMP && ruby test.rb # ⇒ __FILE__ == 'test.rb'
In ruby 2.0 was new builtin __dir__
introduced. It looks like what you are looking for, in case 2.0
-only solution is OK with you.
Related Topics
Rails Form Validation Conditional Bypass
How to Customize Rails Activerecord Validation Error Message to Show Attribute Value
Why Can't I Access a Local Variable Inside a Method in Ruby
How to Get Past "Http://Gems.Rubyforge.Org/ Does Not Appear to Be a Repository" Error Message
How to Extract the Code from a Proc Object
Ruby: Split String at Character, Counting from the Right Side
In Ruby Why Does Nil.Id Return 4
How to Decode Rijndael in Ruby (Encoded in Vb.Net)
Language in a Sandbox in Rails
Error Installing Ruby with Rvm (Osx 10.8)
How to Ftp in Ruby Without First Saving the Text File
Is There a Way in Ruby 1.9 to Remove Invalid Byte Sequences from Strings
How to Disable Db:Schema:Dump for Migrations
How to Find Out Who Is Connected to Actioncable
To_Specs': Could Not Find Chef (>= 0) Amongst [] (Gem::Loaderror)