Need a Rubyistic way to get the full path of files under a directory

Hi,

I am looking for to get the full path of files,under a directory. I did
it as below :

Dir.pwd # => "/home/kirti/ruby"

Dir.glob("*.*")
# => ["SO.rb", "yaml.rb", "URI_examples.rb", "test.rb"]

Dir.glob("*.*").map {|f| File.join(Dir.pwd,f) }
# => ["/home/kirti/ruby/SO.rb",
# "/home/kirti/ruby/yaml.rb",
# "/home/kirti/ruby/URI_examples.rb",
# "/home/kirti/ruby/test.rb"]

But I am looking for any other ways to get it done in Ruby.

Any help/suggestions from you guys?

···

--
Posted via http://www.ruby-forum.com/.

Dir.glob("*.*").map(&File.method(:realpath))

···

--
Posted via http://www.ruby-forum.com/.

From the accepted answer of the link -

···

------
Dir.glob("**/*") # for all files

But I tried -

Dir.glob("**/*")
# => ["sample",
# "sample/test",
# "sample/test/bax.rb",
# "sample/test.txt",
# "so.rb",
# "test.rb"]

The code gives me all files and directories.

Did I mistake any thing above?

---
Dir.glob("**/*/") # for directories

I ran as below :

Dir.glob("**/") # a
# => ["sample/", "sample/test/"]
Dir.glob("**/*/") #b
# => ["sample/", "sample/test/"]

Here is my question - Should I use (a) or (b)

--
Posted via http://www.ruby-forum.com/.

Hans Mackowiak wrote in post #1121573:

Dir.glob("*.*").map(&File.method(:realpath))

Humm Good taste! Thanks...

Dir.glob("*.*").map(&File.method(:realpath))
# => ["/home/kirti/ruby/SO.rb",
# "/home/kirti/ruby/yaml.rb",
# "/home/kirti/ruby/URI_examples.rb",
# "/home/kirti/ruby/test.rb"]
Dir.glob("*.*").map{|f| File.realpath(f)}
# => ["/home/kirti/ruby/SO.rb",
# "/home/kirti/ruby/yaml.rb",
# "/home/kirti/ruby/URI_examples.rb",
# "/home/kirti/ruby/test.rb"]

···

--
Posted via http://www.ruby-forum.com/\.

Hans Mackowiak wrote in post #1121573:

Dir.glob("*.*").map(&File.method(:realpath))

One question! Any difference between `File.realdirpath` and
`File.realpath` ?

Dir.glob("*.*").map{|f| File.realdirpath(f)}
# => ["/home/kirti/ruby/SO.rb",
# "/home/kirti/ruby/yaml.rb",
# "/home/kirti/ruby/URI_examples.rb",
# "/home/kirti/ruby/test.rb"]

···

--
Posted via http://www.ruby-forum.com/\.

You can also use Dir.entires instead of glob.

... or use Pathname

puts Pathname.pwd.entries.map(&:expand_path)

Cheers

robert

···

On Mon, Sep 16, 2013 at 8:49 PM, Hans Mackowiak <lists@ruby-forum.com> wrote:

Dir.glob("*.*").map(&File.method(:realpath))

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Love U Ruby wrote in post #1121576:

Hans Mackowiak wrote in post #1121573:

Dir.glob("*.*").map(&File.method(:realpath))

One question! Any difference between `File.realdirpath` and
`File.realpath` ?

Got the answer from here -

Thanks

···

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote in post #1121612:

···

On Mon, Sep 16, 2013 at 8:49 PM, Hans Mackowiak <lists@ruby-forum.com> > wrote:

Dir.glob("*.*").map(&File.method(:realpath))

You can also use Dir.entires instead of glob.

Thanks for your reply,but it will give also `.` and `..` directories
too.

Can you give me one example of this method Dir# ?

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote in post #1121612:

Dir.glob("*.*").map(&File.method(:realpath))

You can also use Dir.entires instead of glob.

Thanks for your reply,but it will give also `.` and `..` directories
too.

... and globbing with "*.*" will omit all files which do not have a
dot in their name and typically also all entries starting with a dot.

Can you give me one example of this method Dir# ?

Class: Dir (Ruby 2.0.0)

There is
- search engines
- test with IRB

Cheers

robert

···

On Tue, Sep 17, 2013 at 9:18 PM, Love U Ruby <lists@ruby-forum.com> wrote:

On Mon, Sep 16, 2013 at 8:49 PM, Hans Mackowiak <lists@ruby-forum.com> >> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Love U Ruby wrote in post #1121700:

Robert Klemme wrote in post #1121612:

a = Dir.entries(Dir.pwd)
# => ["SO.rb",
# "Selenium-webdriver",
# "yaml.rb",
# "..",
# ".",
# "Nokogiri",
# "URI_examples.rb",
# "csv",
# "test.rb"]

Dir# is giving me back the array,I passed to it. I am not able to
understand the actual use of this method. Can anyone point me there?

···

Dir[*a]
# => ["SO.rb",
# "Selenium-webdriver",
# "yaml.rb",
# "..",
# ".",
# "Nokogiri",
# "URI_examples.rb",
# "csv",
# "test.rb"]

Any help please?

--
Posted via http://www.ruby-forum.com/\.

Robert Klemme wrote in post #1121705:

... and globbing with "*.*" will omit all files which do not have a
dot in their name and typically also all entries starting with a dot.

Thanks for pointing that to me..

Can you give me one example of this method Dir# ?

Class: Dir (Ruby 2.0.0)

There is
- search engines
- test with IRB

I have the example with me.. But that's not much effective to know,when
should I use it. Thus I asked.

···

On Tue, Sep 17, 2013 at 9:18 PM, Love U Ruby <lists@ruby-forum.com>

--
Posted via http://www.ruby-forum.com/\.

Love U Ruby wrote in post #1123140:

Love U Ruby wrote in post #1121700:

Dir# is giving me back the array,I passed to it. I am not able to
understand the actual use of this method. Can anyone point me there?

Dir[*a]

Humm, after spending some time I get now when to use Dir# and
Dir#glob.

Dir['**/'] # => ["sample.txt/", "sample.txt/test/"]

Dir.glob('**/') {|f| p "#{f} is a directory"}
# >> "sample.txt/ is a directory"
# >> "sample.txt/test/ is a directory"

So When I need to do some internal work with the matched files from the
*pattern(here '**/')*,I should use `Dir#glob`,as it supports block.
Otherwise `Dir#` is good to use to get only the entries as per the
pattern.

If I understood wrong,please correct me..

···

--
Posted via http://www.ruby-forum.com/\.

Dir['**/'] is equivalent to Dir.glob('**/',0) (without the block, .glob() returns an array). You can also do:

Dir['**/'].each {|f| …}

which is the same as

Dir.glob('**/') {|f| … }

so, matter of taste, perhaps.

···

On Oct 2, 2013, at 3:38 AM, Love U Ruby <lists@ruby-forum.com> wrote:

Love U Ruby wrote in post #1123140:

Love U Ruby wrote in post #1121700:

Dir# is giving me back the array,I passed to it. I am not able to
understand the actual use of this method. Can anyone point me there?

Dir[*a]

Humm, after spending some time I get now when to use Dir# and
Dir#glob.

Dir['**/'] # => ["sample.txt/", "sample.txt/test/"]

Dir.glob('**/') {|f| p "#{f} is a directory"}
# >> "sample.txt/ is a directory"
# >> "sample.txt/test/ is a directory"

So When I need to do some internal work with the matched files from the
*pattern(here '**/')*,I should use `Dir#glob`,as it supports block.
Otherwise `Dir#` is good to use to get only the entries as per the
pattern.

If I understood wrong,please correct me..

Not exactly: Dir. will always create an Array. Dir.glob() will only
create an Array if no block is given. If there is a block, it is
invoked for each match. That may actually make a difference.

Cheers

robert

···

On Wed, Oct 2, 2013 at 2:59 PM, Tamara Temple <tamouse.lists@gmail.com> wrote:

Dir['**/'].each {|f| …}

which is the same as

Dir.glob('**/') {|f| … }

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Dir['**/'].each {|f| …}

which is the same as

Dir.glob('**/') {|f| … }

Not exactly: Dir. will always create an Array. Dir.glob() will only
create an Array if no block is given. If there is a block, it is
invoked for each match. That may actually make a difference.

That's basically what I said here:

···

On Oct 2, 2013, at 8:41 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Wed, Oct 2, 2013 at 2:59 PM, Tamara Temple <tamouse.lists@gmail.com> wrote:

Dir['**/'] is equivalent to Dir.glob('**/',0) (without the block, .glob() returns an array).

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Dir['**/'].each {|f| …}

which is the same as

Dir.glob('**/') {|f| … }

Not exactly: Dir. will always create an Array. Dir.glob() will only
create an Array if no block is given. If there is a block, it is
invoked for each match. That may actually make a difference.

That's basically what I said here:

Dir['**/'] is equivalent to Dir.glob('**/',0) (without the block, .glob() returns an array).

I was just trying to avoid that your statement

Dir['**/'].each {|f| …}

which is the same as

Dir.glob('**/') {|f| … }

is not misunderstood because it is not the same.

Cheers

robert

···

On Wed, Oct 2, 2013 at 4:01 PM, Tamara Temple <tamouse.lists@gmail.com> wrote:

On Oct 2, 2013, at 8:41 AM, Robert Klemme <shortcutter@googlemail.com> wrote:

On Wed, Oct 2, 2013 at 2:59 PM, Tamara Temple <tamouse.lists@gmail.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/