Forum: Ruby Return actual files only not directories, using Pathname..

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3af9b1c30beee755b32c660d6687537a?d=identicon&s=25 Brian Wallace (Guest)
on 2009-02-05 07:51
(Received via mailing list)
I'm very new to Ruby, but I'm learning pretty quickly ..What i'm trying
to
do is fairly simple...

 I'd like to use Pathname, to return only actual filenames in the
array...
For instance , using FileList.. You can just do this:

FileList["*.*"]

And it will return a string array with the filenames without
directories..

But I rather like Pathname ... Since I can use it like this:

a = Pathname("/Path")
b = a.children

puts b[1]   #==>  "/Path/file1.txt"

b[1].read   #==>  "This is data in a file named file1.txt"
puts b[2] #==> "/Path/file2.txt"
.....

Does anyone have any tips on how I could use Pathname to return files
only,
and not directories?

 Thanks,

Brian
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2009-02-05 08:58
Brian Wallace wrote:

>  I'd like to use Pathname, to return only actual filenames in the
> array...
> For instance , using FileList.. You can just do this:
>
> FileList["*.*"]
>

FileList?  I can't find anything in the ruby docs about FileList.
google doesn't help either.


> Does anyone have any tips on how I could use Pathname to return files
> only,
> and not directories?

Just separate out the files after you retrieve all the file names:

require "pathname"

pn_obj = Pathname.new("./")
file_objs = pn_obj.children

dirs = []
files = []

file_objs.each do |obj|
  if obj.directory?
    dirs << obj
  else
    files << obj
  end
end
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2009-02-05 09:01
(Received via mailing list)
On Thu, Feb 5, 2009 at 1:50 AM, Brian Wallace <draygen80@gmail.com>
wrote:

> Does anyone have any tips on how I could use Pathname to return files only,
> and not directories?

Use Pathname.glob

>> require "pathname"
=> true
>> Pathname.glob("lib/**/*.rb")
=> [#<Pathname:lib/prawn/compatibility.rb>,
#<Pathname:lib/prawn/document/annotations.rb>,
#<Pathname:lib/prawn/document/bounding_box.rb>,
#<Pathname:lib/prawn/document/destinations.rb>,
#<Pathname:lib/prawn/document/internals.rb>,
#<Pathname:lib/prawn/document/page_geometry.rb>,
#<Pathname:lib/prawn/document/span.rb>,
#<Pathname:lib/prawn/document/text/box.rb>,
#<Pathname:lib/prawn/document/text/wrapping.rb>,
#<Pathname:lib/prawn/document/text.rb>,
#<Pathname:lib/prawn/document.rb>, #<Pathname:lib/prawn/encoding.rb>,
#<Pathname:lib/prawn/errors.rb>, #<Pathname:lib/prawn/font/afm.rb>,
#<Pathname:lib/prawn/font/dfont.rb>,
#<Pathname:lib/prawn/font/ttf.rb>, #<Pathname:lib/prawn/font.rb>,
#<Pathname:lib/prawn/graphics/color.rb>,
#<Pathname:lib/prawn/graphics.rb>,
#<Pathname:lib/prawn/images/jpg.rb>,
#<Pathname:lib/prawn/images/png.rb>, #<Pathname:lib/prawn/images.rb>,
#<Pathname:lib/prawn/literal_string.rb>,
#<Pathname:lib/prawn/measurement_extensions.rb>,
#<Pathname:lib/prawn/measurements.rb>,
#<Pathname:lib/prawn/name_tree.rb>,
#<Pathname:lib/prawn/pdf_object.rb>,
#<Pathname:lib/prawn/reference.rb>, #<Pathname:lib/prawn.rb>]
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2009-02-05 09:06
(Received via mailing list)
From: Brian Wallace [mailto:draygen80@gmail.com]
#  I'd like to use Pathname,

Pathname is a very nice façade for a lot of file things ;)

# to return only actual filenames in the array...
# But I rather like Pathname ... Since I can use it like this:
# a = Pathname("/Path")
# b = a.children
# puts b[1]   #==>  "/Path/file1.txt"
# b[1].read   #==>  "This is data in a file named file1.txt"
# puts b[2] #==> "/Path/file2.txt"
# .....

try passing a boolean arg to children, eg

  b = a.children(false)



# Does anyone have any tips on how I could use Pathname to
# return files only, and not directories?

*nixism, they're simply all files, you'll have ask more.

try #file? or #directory?, eg

   b[1].file?

   b[1].directory?


warning: be careful when mixing result of #children(false) with other
pathname methods.

on your case, to get only the files (non-dir ie), i'd probably just do,

  b = a.children.select do |path|
        path.file?
      end

and to get a list of the names (as strings ie)

  b.map do |path|
    path.basename.to_s
  end


and if you want to get a separate list of files and dirs, try partition,
eg

  files, dirs = a.children.partition do |path|
                  path.file?
                end
D9d7afc2a6bd87c96532f91da29553f1?d=identicon&s=25 Sascha Abel (daemor)
on 2009-02-05 10:52
Gregory Brown wrote:
> On Thu, Feb 5, 2009 at 1:50 AM, Brian Wallace <draygen80@gmail.com>
> wrote:
>
>> Does anyone have any tips on how I could use Pathname to return files only,
>> and not directories?
>
> Use Pathname.glob
>
>>> require "pathname"
> => true
>>> Pathname.glob("lib/**/*.rb")

You will have to be cautious with this one though, since a directory
name may contain dots such as 'project.rb'. I guess it's highly unusual
but I have seen this before ...
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2009-02-05 16:05
(Received via mailing list)
On Feb 5, 2009, at 1:58 AM, 7stud -- wrote:

> google doesn't help either.
It's part of Rake.

James Edward Gray II
This topic is locked and can not be replied to.