Class: Path
Class Method Summary collapse
Instance Method Summary collapse
- #+(obj) ⇒ Object
- #/(obj) ⇒ Object
- #directory ⇒ Object
- #directory? ⇒ Boolean
- #exist? ⇒ Boolean (also: #exists?)
- #extension ⇒ Object
- #file ⇒ Object
- #file? ⇒ Boolean
- #filename ⇒ Object
- #files(pattern = "*") ⇒ Object
-
#initialize(string = Path.root) ⇒ Path
constructor
A new instance of Path.
- #inspect ⇒ Object
- #read ⇒ Object
Methods inherited from String
Constructor Details
#initialize(string = Path.root) ⇒ Path
Returns a new instance of Path.
3 4 5 |
# File 'lib/camino.rb', line 3 def initialize(string = Path.root) super(string) end |
Class Method Details
.home ⇒ Object
72 73 74 |
# File 'lib/camino.rb', line 72 def self.home Path.new(ENV['HOME']) end |
.pwd ⇒ Object
80 81 82 |
# File 'lib/camino.rb', line 80 def self.pwd Path.new(Dir.pwd) end |
.relative ⇒ Object
65 66 67 68 69 70 |
# File 'lib/camino.rb', line 65 def self.relative path = get_path_from_caller(caller) dir = File.dirname(path) raise "Path doesn't seem to exist:#{last_call.inspect}" if !File.exist?(dir) Path.new(dir) end |
.root ⇒ Object
76 77 78 |
# File 'lib/camino.rb', line 76 def self.root Path.new("/") end |
Instance Method Details
#+(obj) ⇒ Object
11 12 13 |
# File 'lib/camino.rb', line 11 def +(obj) Path.new(self.to_s + obj.to_s) end |
#/(obj) ⇒ Object
7 8 9 |
# File 'lib/camino.rb', line 7 def /(obj) Path.new(File.join(self, obj.to_s)) end |
#directory ⇒ Object
45 46 47 |
# File 'lib/camino.rb', line 45 def directory File.dirname(self) end |
#directory? ⇒ Boolean
21 22 23 |
# File 'lib/camino.rb', line 21 def directory? File.directory?(self) end |
#exist? ⇒ Boolean Also known as: exists?
15 16 17 |
# File 'lib/camino.rb', line 15 def exist? File.exists?(self) end |
#extension ⇒ Object
53 54 55 |
# File 'lib/camino.rb', line 53 def extension File.extname(self)[1..-1] end |
#file ⇒ Object
49 50 51 |
# File 'lib/camino.rb', line 49 def file File.basename(self) end |
#file? ⇒ Boolean
25 26 27 |
# File 'lib/camino.rb', line 25 def file? exist? && !directory? end |
#filename ⇒ Object
57 58 59 |
# File 'lib/camino.rb', line 57 def filename File.basename(self, File.extname(self)) end |
#files(pattern = "*") ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/camino.rb', line 29 def files(pattern = "*") if directory? Dir.glob(File.join(self, pattern)).collect{|p|Path.new(p)} else raise "Not a directory:(#{self})" end end |
#inspect ⇒ Object
61 62 63 |
# File 'lib/camino.rb', line 61 def inspect "#<Path:#{(object_id * 2).to_s(16)}:#{self}>" end |
#read ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/camino.rb', line 37 def read if file? File.open(self).read else raise "Not a file:(#{self})" end end |