Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Return The Device Name

DZone's Guide to

Return The Device Name

·
Free Resource
This will return the name of the device on which the file resides on ...


class File::Stat
  class << self
    def device_name(file)
      Dir['/dev/*'].inject({}) { |h, v|
        h.update(File.stat(v).rdev => v)
      }.values_at(File.stat(file).dev).first || nil
    end
  end
end


Simple use-case:


(main):001:0> File::Stat.device_name('/etc/resolv.conf')
=> "/dev/sda3"


Please note that this code may not be portable enough as it relies on the content of /dev. 
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}