Rafa_F
September 16, 2013, 8:14pm
#1
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?
my-ruby
September 16, 2013, 8:49pm
#2
Dir.glob(". ").map(&File.method(:realpath))
my-ruby
September 16, 2013, 9:07pm
#3
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”]
my-ruby
September 16, 2013, 9:17pm
#4
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”]
my-ruby
September 17, 2013, 8:02am
#5
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
my-ruby
September 17, 2013, 9:18pm
#6
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#[] ?
http://www.ruby-doc.org/core-2.0.0/Dir.html#method-c-5B-5D
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?
my-ruby
September 17, 2013, 10:00pm
#7
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#[] ?
http://www.ruby-doc.org/core-2.0.0/Dir.html#method-c-5B-5D
There is
search engines
test with IRB
Cheers
robert
my-ruby
September 16, 2013, 9:25pm
#8
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 -
http://stackoverflow.com/questions/14569044/confusion-with-ruby-file-class-related-path-methods
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?
my-ruby
October 2, 2013, 10:38am
#10
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…
my-ruby
September 18, 2013, 10:51am
#11
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#[] ?
http://www.ruby-doc.org/core-2.0.0/Dir.html#method-c-5B-5D
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.
my-ruby
October 2, 2013, 3:42pm
#12
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
my-ruby
October 2, 2013, 3:00pm
#13
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.
my-ruby
October 2, 2013, 4:01pm
#14
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).
my-ruby
October 2, 2013, 4:36pm
#15
From the accepted answer of the link -
http://stackoverflow.com/questions/2370702/one-liner-to-recursively-list-directories-in-ruby
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)
my-ruby
October 2, 2013, 5:20pm
#16
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