user391986
user391986

Reputation: 30956

Getting the true work directory when running ruby through an alias

I created an alias so that I can run my ruby script from any directory.

alias run_me="ruby ~/mycli/script.rb"

However now the File.dirname doesn't work correctly. When my script runs from the alias, and executes File.dirname(File.realpath(__FILE__)), I always get the wrong directory. I get the directory of the ruby script file but not where my terminal actually is.


The following code outputs ~/mycli but I was expecting ~/some_random_directory. How can I change this behavior?

cd ~/some_random_directory
run_me 

Upvotes: 1

Views: 50

Answers (1)

Cyrus
Cyrus

Reputation: 88999

I suggest:

File.basename(Dir.getwd)

Upvotes: 1

Related Questions