How to set up a Ruby project structure on Ubuntu 16.10

Ruby language logo

For about two months now I’ve been learning how to code in Ruby using Learn Ruby the Hard Way. Never mind the title, but it’s actually a very good resource for learning how to program in Ruby. The last few exercises that I’ve gone through could have been written better, but overall, it’s a good book for those just getting into the world of software coding.

Exercise 46 of the book, which deals with how to set up a skeleton directory for a Ruby project, shows how to go about it manually. With the right tools installed, however, the same task can be completed automatically, which is what you’re going to learn how to do in this article.

It will help if you have Git installed already. If not, use this article to install it on your favorite Linux distribution, then come back here to continue with this tutorial.

Welcome back!

To begin, you’ll need to install a couple of applications – ruby-bundler and ruby-rspec-core. The former helps you manage Ruby application dependencies and gives you access to the bundle and bundler commands, while the latter gives you access to rspec, a standalone test runner for Ruby RSpec test suites. RSpec is a testing tool for Ruby. Use the command shown below to install both applications.

# Install the following

sudo apt install ruby-bundler ruby-rspec-core

#

With those two installed, you are now ready to create your project’s skeleton structure, which you do with the following command:

# To create a Ruby project skeleton type
# Replace projectA with the name of your project
# This command will fail if Git is not installed

bundle gem projectA

#

After running that command, you’ll be asked the following question. My comments come before each question. Note that your answer to the questions will be applied automatically to future projects you create using the bundle gem command above.

# The name of the project is ProjectA. Yours, of course, will be different
# My answer to this question was "rspec". I've never used "minitest", but nothing stops you from using it
 
 Creating gem 'projectA'...
Do you want to generate tests with your gem?
Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none):  

# If you answer "y" here, a license file called LICENSE.txt will be populated with the MIT license. Otherwise,
# a license file will not be created 

Do you want to license your code permissively under the MIT license?
This means that any other developer or company will be legally allowed to use your code for free as long as 
they admit you created it. You can read more about the MIT license at 
http://choosealicense.com/licenses/mit. y/(n):

# With a "y" answer to this question, a code of conduct file called CODE_OF_CONDUCT.md will be created. Otherwise, 
# it will not be created

Do you want to include a code of conduct in gems you generate?
Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. 
You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing 
to the responsibility of enforcing it, so be sure that you are prepared to do that. Be sure that your email 
address is specified as a contact in the generated code of conduct so that people know who to contact in case 
of a violation. For suggestions about how to enforce codes of conduct, see http://bit.ly/coc-enforcement. y/(n):

#

Once you’ve answered that last question, your project’s files will be generated. The final output will look like this:

# This is the final output of the bundle gem command

Code of conduct enabled in config
      create  projectA/Gemfile
      create  projectA/.gitignore
      create  projectA/lib/projectA.rb
      create  projectA/lib/projectA/version.rb
      create  projectA/projectA.gemspec
      create  projectA/Rakefile
      create  projectA/README.md
      create  projectA/bin/console
      create  projectA/bin/setup
      create  projectA/.travis.yml
      create  projectA/.rspec
      create  projectA/spec/spec_helper.rb
      create  projectA/spec/projectA_spec.rb
      create  projectA/CODE_OF_CONDUCT.md
Initializing git repo in /home/kamit/projectA

#

Now you can start editing those files and fill in items specific to your project. If Git was properly set up with your credentials using this article, your gemspec file should be populated with the global username and email address you configured for Git. Edit that file to fill in the rest of the information specific to your project.

Related Post:  How to dual-boot Windows 7 and Chakra Linux Archimedes, with shared NTFS partition at the end

So there you have it – a skeleton structure for your Ruby project.

Related Post:  How to change the height and position of the KDE panel

How do you run a basic test using RSpec? Change into the project’s directory and type the following command:

# To run a test, you must be in the project's directory, so change into it
# Replace "projectA" with the name of your project

cd projectA

# Then run your first test
# It might fail if your gemspec file is missing vital information

rspec

# The output should be of this sort
# > indicates output

> No examples found.


> Finished in 0.00033 seconds (files took 0.0627 seconds to load)
> 0 examples, 0 failures

#

Ruby language logo

Share:

Share on facebook
Facebook
Share on twitter
Twitter
Share on pinterest
Pinterest
Share on linkedin
LinkedIn

Hola! Did you notice that LinuxBSDos.com no longer run network ads?  Yep, no more ads from the usual suspects that track and annoy you across the Internet. But since I still need to pay to keep the site running, feel free to make a small donation by PayPal or your favorite cryptocurrency.

  • Bitcoin
  • Ethereum
  • Xrp
  • Bitcoin cash
  • Bitcoin sv
  • Litecoin
  • Binance coin
  • Cardano
  • Ethereum classic
Scan to Donate Bitcoin to bc1qzvlte2m224zkayhdc7fdfjkp2rsgt0l5a496ua

Donate Bitcoin to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin

Scan to Donate Ethereum to 0x0F4362DFF77F3Ba0Dc637F5f3Eba35D09a2fA60C

Donate Ethereum to this address

Scan the QR code or copy the address below into your wallet to send some Ethereum

Scan to Donate Xrp to r4ggjvL36njsMCYTkJ3S7cTHscPsMsSGQv

Donate Xrp to this address

Scan the QR code or copy the address below into your wallet to send some Xrp

Scan to Donate Bitcoin cash to qrs0dedzp9t55af3nfwypydghp29r0xguy9s20fz2k

Donate Bitcoin cash to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin cash

Scan to Donate Bitcoin sv to 15K9TLyVDBtLuG9cYvXCX9SSkq9C9oUKHK

Donate Bitcoin sv to this address

Scan the QR code or copy the address below into your wallet to send some Bitcoin sv

Scan to Donate Litecoin to LetJ9QQMb7u2LMZ9Tu6rtHwcBcQFW98fbG

Donate Litecoin to this address

Scan the QR code or copy the address below into your wallet to send some Litecoin

Scan to Donate Binance coin to bnb1ga8trq08ssqepd90v6225nzfgy448pu5pw8gxp

Donate Binance coin to this address

Scan the QR code or copy the address below into your wallet to send some Binance coin

Scan to Donate Cardano to addr1qx2354yw49etstfljpdhwja3ajjlt487lg95vu9ngy2q6vu4rf2ga2tjhqknlyzmwa9mrm997h20a7stgectxsg5p5esq5l7d9

Donate Cardano to this address

Scan the QR code or copy the address below into your wallet to send some Cardano

Scan to Donate Ethereum classic to 0xcD6CC972a2297FcafACDcfE042C55C69516a9264

Donate Ethereum classic to this address

Scan the QR code or copy the address below into your wallet to send some Ethereum classic

Subscribe for updates. Trust me, no spam!

Sponsored links

1. Attend Algorithm Conference, a top AI and ML event.
2. Reasons to use control panel for your server.
3. DHgate Computers Electronics, Cell Phones & more.

Upcoming events

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest

On social media
Via my newsletter

Partner links

1. Attend Algorithm Conference, a top AI and ML event for 2021.
2. Reasons to use control panel for your server.
3. DHgate Computers Electronics, Cell Phones & more.
Hacking, pentesting distributions

Linux Distributions for Hacking

Experts use these Linux distributions for hacking, digital forensics, and pentesting.

Categories
Archives

The authors of these books are confirmed to speak during

Algorithm Conference

T-minus AI

Author was the first chairperson of AI for the U.S. Air Force.

The case for killer robots

Author is the Director of the Center for Natural and Artificial Intelligence.

Why greatness cannot be planned

Author works on AI safety as a Senior Research Scientist at Uber AI Labs.