This post originated from an RSS feed registered with Ruby Buzz
by Red Handed.
Original Post: How Powerful Pathname Is
Feed Title: RedHanded
Feed URL: http://redhanded.hobix.com/index.xml
Feed Description: sneaking Ruby through the system
How powerful Pathname is! Unfortunately I did not know. Have you ever been
annoyed which class to use, File, FileTest, Dir or Find? Use Pathname, instead.
That’s all (on UNIX platforms). The Pathname class, included in the Ruby 1.8
standard libraries, represents paths of directories and files, and is a facade
to File, FileTest, Dir and Find.
You can see some examples in the header lines of the source:
require 'pathname'
p = Pathname.new("/usr/bin/ruby")
size = p.size # 27662
isdir = p.directory? # false
dir = p.dirname # Pathname:/usr/bin
base = p.basename # Pathname:ruby
dir, base = p.split # [Pathname:/usr/bin, Pathname:ruby]
data = p.read
p.open { |f| _ }
p.each_line { |line| _ }
That means
p = "/usr/bin/ruby"
size = File.size(p) # 27662
isdir = File.directory?(p) # false
dir = File.dirname(p) # "/usr/bin"
base = File.basename(p) # "ruby"
dir, base = File.split(p) # ["/usr/bin", "ruby"]
data = File.read(p)
File.open(p) { |f| _ }
File.foreach(p) { |line| _ }
Pathname has exist?, file?, mkdir, rmdir, rmtree, children, find and so on as well.