harp:~ > cat a.rb
require 'pathname'
pn = Pathname.new __FILE__
pn = pn.expand_path
p pn.dirname
p pn.basename
tmp = Pathname.new '/tmp'
p pn.relative_path_from(tmp)
harp:~ > ruby a.rb
#<Pathname:/home/ahoward>
#<Pathname:a.rb>
#<Pathname:../home/ahoward/a.rb>
and a whole lot more:
harp:~ > ri Pathname
-------------------------------------------------------- Class: Pathname
Pathname
···
On Tue, 9 May 2006, Peter Szinek wrote:
Hello all,
I am new to Ruby etc. etc. (all the usual disclaimer about how i can not code in Ruby yet etc), so probably because of this i was not able to find something like
myFile.basename
resp
myFile.anything_which_returns_a_sensible_filename_like_string
and not
File.basename(myFile)
which i find too verbose for Ruby.
By browsing the reference part of the PickAxe 2nd ed, i did not find any alternative to File.basename. Can somebody help me with this?
Thx,
Peter
--------
Pathname represents a pathname which locates a file in a
filesystem. It supports only Unix style pathnames. It does not
represent the file itself. A Pathname can be relative or absolute.
It's not until you try to reference the file that it even matters
whether the file exists or not.
Pathname is immutable. It has no method for destructive update.
The value of this class is to manipulate file path information in a
neater way than standard Ruby provides. The examples below
demonstrate the difference. *All* functionality from File,
FileTest, and some from Dir and FileUtils is included, in an
unsurprising way. It is essentially a facade for all of these, and
more.
Examples
--------
Example 1: Using Pathname
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| _ }
Example 2: Using standard Ruby
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| _ }
Example 3: Special features
p1 = Pathname.new("/usr/lib") # Pathname:/usr/lib
p2 = p1 + "ruby/1.8" # Pathname:/usr/lib/ruby/1.8
p3 = p1.parent # Pathname:/usr
p4 = p2.relative_path_from(p3) # Pathname:lib/ruby/1.8
pwd = Pathname.pwd # Pathname:/home/gavin
pwd.absolute? # true
p5 = Pathname.new "." # Pathname:.
p5 = p5 + "music/../articles" # Pathname:music/../articles
p5.cleanpath # Pathname:articles
p5.realpath # Pathname:/home/gavin/articles
p5.children # [Pathname:/home/gavin/articles/linux, ...]
Breakdown of functionality
--------------------------
Core methods
These methods are effectively manipulating a String, because that's
all a path is. Except for #mountpoint?, #children, and #realpath,
they don't access the filesystem.
* +
regards.
-a
--
be kind whenever possible... it is always possible.
- h.h. the 14th dali lama