The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
How Powerful Pathname Is

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Red Handed

Posts: 1158
Nickname: redhanded
Registered: Dec, 2004

Red Handed is a Ruby-focused group blog.
How Powerful Pathname Is Posted: Jun 8, 2005 9:42 AM
Reply to this message Reply

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
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Red Handed
Latest Posts From RedHanded

Advertisement

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.

Read: How Powerful Pathname Is

Topic: Holy Red Snakes! (Cont'd.) Previous Topic   Next Topic Topic: RDBMS' are slow

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use