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?

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

Hans M. 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”]

Hans M. 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”]

On Mon, Sep 16, 2013 at 8:49 PM, Hans M. [email protected]
wrote:

Dir.glob(“.”).map(&File.method(:realpath))

You can also use Dir.entires instead of glob.

… or use Pathname

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

Cheers

robert

Robert K. wrote in post #1121612:

On Mon, Sep 16, 2013 at 8:49 PM, Hans M. [email protected]
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#[] ?

Actually the example I have,not help me to understand the usefulness of
the method.

a = Dir.entries(Dir.pwd)

=> [“SO.rb”,

“Selenium-webdriver”,

“yaml.rb”,

“…”,

“.”,

“Nokogiri”,

“URI_examples.rb”,

“csv”,

“test.rb”]

Dir[*a]

=> [“SO.rb”,

“Selenium-webdriver”,

“yaml.rb”,

“…”,

“.”,

“Nokogiri”,

“URI_examples.rb”,

“csv”,

“test.rb”]

Any help please?

On Tue, Sep 17, 2013 at 9:18 PM, Love U Ruby [email protected]
wrote:

Robert K. wrote in post #1121612:

On Mon, Sep 16, 2013 at 8:49 PM, Hans M. [email protected]
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.

… 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

Love U Ruby wrote in post #1121576:

Hans M. 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

Love U Ruby wrote in post #1121700:

Robert K. 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?

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…

Robert K. wrote in post #1121705:

On Tue, Sep 17, 2013 at 9:18 PM, Love U Ruby [email protected]

… 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 Wed, Oct 2, 2013 at 2:59 PM, Tamara T. [email protected]
wrote:

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.

Cheers

robert

On Oct 2, 2013, at 3:38 AM, Love U Ruby [email protected] wrote:

Dir#glob.
pattern.

If I understood wrong,please correct me…

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 8:41 AM, Robert K. [email protected]
wrote:

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).

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)

On Wed, Oct 2, 2013 at 4:01 PM, Tamara T. [email protected]
wrote:

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