Greg K
Greg K

Reputation: 11120

Recommended layout for Ruby CLI application

Firstly, I'm not using rails. This is vanilla ruby application. I've read about packaging a CLI ruby application as a GEM.

So I guess my question would be, is this the ruby way? Does this layout lend itself to class autoloading?

I'm coming from a PHP background where I'm used to application layouts that adhere to PSR-0 style (see examples section).

Upvotes: 4

Views: 1787

Answers (2)

Yuri Karpovich
Yuri Karpovich

Reputation: 402

Actually you can execute any ruby file from command-line interface (CLI) using console_runner gem. All you need is to add annotations (YARD-like syntax) to your Ruby code and then execute it from command line:

$ c_run /path/your_file.rb say_hello

/path/your_file.rb:

# @runnable
class MyClass

    # @runnable
    def say_hello
      puts 'Hello!'
    end

end

Upvotes: 1

davetron5000
davetron5000

Reputation: 24841

Yes, the way to build and distribute a Ruby command-line app is more or less as that article describes:

  • bin - exe goes here
  • lib/your_app.rb - requires all files under lib/your_app
  • lib/your_app/whatever.rb - modules and files to build your command-line app
  • your_app.gemspec - gemspec; make sure you mention that there's a bin file
  • Rakefile - manage development
  • test - yup, tests

It's completely OK to break up your app into classes and modules inside lib. By distributing with RubyGems the command-line app will be in the user's path and it will have access to everything in lib.

RubyGems has first-class support for distributing command-line apps; it's not just for libraries.

Upvotes: 2

Related Questions