Created
February 24, 2012 17:35
-
-
Save marcinwyszynski/1902275 to your computer and use it in GitHub Desktop.
Shell built-in 'pwd' implemented in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
=begin | |
The concept here is to print out the current working | |
directory (pwd) following i-nodes only. | |
=end | |
def pwd | |
path = [] | |
loop do | |
self_inode = File::stat('.').ino | |
parent_inode = File::stat('..').ino | |
Dir::chdir('..') | |
name = Dir['*'].entries.select do |e| | |
File::stat(e).ino == self_inode | |
end.first | |
break if self_inode == parent_inode | |
path.unshift(name) | |
end | |
return '/' + path.join('/') | |
end | |
if __FILE__ == $0 | |
puts pwd | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment