Get Current Directory

10 views
Skip to first unread message

BeeRich33

unread,
Sep 6, 2017, 6:11:21 AM9/6/17
to sinatrarb
I'm looking for a way for Sinatra to get filenames in the models, routes, and helpers directories.

app.rb
helpers
  _init
.rb
  alpha
.rb
  bravo
.rb
models
  _init
.rb
  gulf
.rb
  hotel
.rb
routes
  _init
.rb
  kilo
.rb
  lima
.rb


So in helpers/_init.rb I want to build an array of files for requiring:

.
..
_init
.rb
alpha
.rb
bravo
.rb


reqs = Dir.entries('helpers').select {|i| i =~ /^[a-z]+.rb/}



I'd like to abstract this as much as I can, and make sure I'm using the right directory.  Because this is a required with no direct output to anything I can really see, I'm winging it.  Would be nice to know how to output to the MacOS console, if you know how that is done, that would be excellent.  

Instead of ('models'), I'm trying Dir.getwd and it's coughing up an error.  

cannot load such file -- /Library/WebServer/Documents/usersys/models/app (LoadError)

So it looks like Dir.getwd is resolving to the site root.  Is there no way to have models/_init.rb know it's in the models directory?

Cheers

Keith Bennett

unread,
Sep 6, 2017, 10:40:59 PM9/6/17
to sina...@googlegroups.com
If I understand you correctly...

Get all .rb files in the models directory:

Dir['models/*.rb']

Construct a filespec relative to the directory in which the code's currently running file lives:

File.join(File.basename(__FILE__), ...)
(note: this will not work in irb)

- Keith


--
You received this message because you are subscribed to the Google Groups "sinatrarb" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sinatrarb+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Keith Bennett

unread,
Sep 7, 2017, 4:49:32 AM9/7/17
to sina...@googlegroups.com
Sorry, I meant dirname, not basename:

File.join(File.dirname(__FILE__), ...)

- Keith

Reply all
Reply to author
Forward
0 new messages