Skip to content

Instantly share code, notes, and snippets.

@marcinwyszynski
Created February 24, 2012 17:35
Show Gist options
  • Save marcinwyszynski/1902275 to your computer and use it in GitHub Desktop.
Save marcinwyszynski/1902275 to your computer and use it in GitHub Desktop.
Shell built-in 'pwd' implemented in Ruby
#!/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