I have rails application, I need see files in any folder. example I have application on c:\rails_app and in controller write this code:
@files = Dir.glob("Z:/*")
and don't see file entire directory
in rails console work perfectly.
I have question:
How to see files from non rails directory?
Thanks
--- after some answer
Problem not in use Dir class. Problem that Rails see only own root directory and i can`t change dir to other disk or folder in rails controller.
You can use Dir#entries
, Dir#glob
or Dir#[]
to get a listing in any folder.
Dir.entries('/Users/ccashwell/.vim/')
=> [".",
"..",
".git",
".gitignore",
".gitmodules",
".netrwhist",
"ackrc",
"after",
"autoload",
"bundle",
"init",
"LICENSE",
"README.md",
"snippets",
"syntax",
"vimrc"]
Dir.glob('/Users/ccashwell/.vim/*')
=> ["/Users/ccashwell/.vim/ackrc",
"/Users/ccashwell/.vim/after",
"/Users/ccashwell/.vim/autoload",
"/Users/ccashwell/.vim/bundle",
"/Users/ccashwell/.vim/init",
"/Users/ccashwell/.vim/LICENSE",
"/Users/ccashwell/.vim/README.md",
"/Users/ccashwell/.vim/snippets",
"/Users/ccashwell/.vim/syntax",
"/Users/ccashwell/.vim/vimrc"]
Dir['/Users/ccashwell/.vim/*']
=> ["/Users/ccashwell/.vim/ackrc",
"/Users/ccashwell/.vim/after",
"/Users/ccashwell/.vim/autoload",
"/Users/ccashwell/.vim/bundle",
"/Users/ccashwell/.vim/init",
"/Users/ccashwell/.vim/LICENSE",
"/Users/ccashwell/.vim/README.md",
"/Users/ccashwell/.vim/snippets",
"/Users/ccashwell/.vim/syntax",
"/Users/ccashwell/.vim/vimrc"]
The equivalent of ls
(list files) from the rails console is
Dir.entries(Dir.pwd)
e.g.
Dir.entries(Dir.pwd)
# Returns this:
=> [".", "..", ".DS_Store", "app", ".ruby-version", "test", "bin", "config", "config.ru",
"storage", "README.md", "Rakefile", "public", ".gitignore", "package.json", "lib", "db",
"Gemfile", "log", "Gemfile.lock", "init.R", ".git", "tmp", "vendor"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With