0

im brand new to Ruby and I just finished coding a program.

the program has about 350 lines of code, 6 methods but no classes. it uses constants and global variables.

Is it convention to, for example, have the global variables/constants in a seperate file? should I split all the methods into different files?

What should the folders be like where I have all the files?

and Lastly, how do i create a makefile (or rakefile whatever it's called. i have the rake rubygem)

thank you very much

  • 1
    That's far to general a question and would take a whole book to cover the answers. Please be VERY VERY specific with your questions. https://www.ruby-lang.org/en/documentation/quickstart/ might help – jamesc Jul 04 '16 at 04:57
  • Nice answer http://stackoverflow.com/questions/3307209/what-is-the-modern-way-to-structure-a-ruby-gem – Farkhat Mikhalko Jul 04 '16 at 05:12
  • 1
    It's best to ask one question per question. – Wayne Conrad Jul 04 '16 at 14:39

3 Answers3

1

For a beginner the following would be enough.

├── Gemfile
├── Gemfile.lock
├── app.rb
├── config.rb
└── lib/
    ├── lib1.rb
    ├── lib2.rb

where app.rb is you main file and you run the project using

ruby app.rb

lib/ has you library files lib1.rb and lib2.rb

suppose you are tweeting through your app then lib1.rb could be tweet.rb

levininja
  • 3,118
  • 5
  • 26
  • 41
krazedkrish
  • 638
  • 3
  • 10
  • ok thanks for this, but i only have 1 file right now. which is just program.rb and i run it using `ruby program.rb` in the terminal. i dont have any config or lib files. do i need to create a rakefile too or is `ruby program.rb` enough? – tony_landoza Jul 04 '16 at 05:14
  • no you dont need a rakefile, unless you feel you really need it. – krazedkrish Jul 04 '16 at 09:18
0

I recommend using Hoe: https://github.com/seattlerb/hoe

Hoe focuses on keeping everything in its place in a useful form and intelligently extracting what it needs. As a result, there are no extra YAML files, config directories, ruby files, or any other artifacts in your release that you wouldn't already have.

What I like is that the test directory is setup by default, and running rake will run the tests. I require my lib/project_name.rb and fill in the tests.

A Hoe setup includes a Rakefile. Rake is maintained by the same group behind Hoe, Seattle.rb. Info: https://github.com/ruby/rake; docs: http://docs.seattlerb.org/rake/

Hoe allows you to turn your project into a Gem if applicable to your use case.

Hoe has a bunch of plugins:

Hoe has a flexible plugin system that allows you to activate and deactivate what tasks are available on a given project. Hoe has been broken up into plugins partially to make maintenance easier but also to make it easier to turn off or replace code you don't want.

Plus it's written by hardcore rubyists Ryan Davis (author of MiniTest) and Eric Hodel, so the code is bulletproof and the repo stays maintained.


Structure Overview

project_dir/
  History.txt
  Manifest.txt
  README.txt
  Rakefile
  bin/...
  lib/...
  test/...

Installation & Setup

Install via command line:

gem install hoe

Setup a project:

sow project_name

Documentation

Hoe Documentation PDF

SoAwesomeMan
  • 3,226
  • 1
  • 22
  • 25
  • 1
    great answer, very informative. so thanks very much for that. but is all this still applicable if i only have one file (program.rb) and i run in in terminal using `ruby program.rb`? do i still need a rakefile or a lib folder/files? – tony_landoza Jul 04 '16 at 08:08
  • It depends. What are you trying to do? If this is code for anything professional I would recommend using a directory structure with a _"rakefile or a lib folder/files"_. Check out this minimal repo to get an idea of how you could use Hoe to structure your code: https://github.com/step1profit/chef_steps – SoAwesomeMan Jul 04 '16 at 08:20
0

"It uses constants and global variables." Oh, no, please, don't do this. You're making your life needlessly difficult.

You really need to avoid using global constants and variables unless you literally have no other option. They're extremely difficult to keep orderly and tend to cause chaos in any non-trivial program.

Organize everything in your program into one or more module or class definitions. Give them meaningful names. You'll use a module for "collection of method" containers, class for something that encapsulates a process or an entity of some sort.

This distinction is sometimes hard to make, but thinking about the problem carefully and trying different approaches will be necessary to do it properly.

The basics of my Ruby code are a directory called bin/ for any sort of executables, lib/ for the library files, test/ for testing code, and a README file to explain what it all does. The last two are especially important as untested code is nothing but trouble, and undocumented code is even worse.

You'll only need a Rakefile if you have some kind of processes you need to perform on a routine basis. Packaging a gem is one such thing, or as with Rails, managing database migrations. Most projects don't need one of these, but it's sometimes helpful to include one that simply runs tests or produces documentation.

The format of a Rakefile is super simple, and there are literally millions of examples out there. Find one that does what you need and adapt it to your particular structure.

tadman
  • 208,517
  • 23
  • 234
  • 262