Learn Ruby the Hard Way – Exercise 46 A Project Skeleton

Here is my work for Exercise 46.

I put my project skeleton on Github.

Study Drills
1. Read about how to use all of the things you installed.
A Ruby “gem” is a self-contained library or program. It is distributed by RubyGems, a packet manager. RubyGems makes it easy to install gems and to distribute gems. A gem contains code, documentation, and a gemspec.

NAME.gemspec
– a gemspec is a file that contains information for a gem.
bin – bin stands for binaries, it is where the binary (executable) files go!
(bin does NOT stand for ‘garbage bin’ as previously thought…
remember when the trash used to have a /usr/local/bin path?)
doc – this directory is where you put the documentation for your gem
lib – this directory is where you put the code for the gem.
Rakefile – this is the file that the rake program uses to automate tests and generate code
data – this directory is a place to store application data (for example, database, cache, session files).
ext – this directory is for the extensions of your application.
tests – this directory is where you put the tests for the gem

2. Read about the NAME.gemspec file, commonly called a “Gemspec”, and all it has to offer.
See above. We can use a gemspec file to build a Ruby gem.

3. Make a project put code into the module, then get the module working. This means you have to change all the files, directories, and modules with NAME in them to the name of your project.
I copied the skeleton directory, then changed all the files and directory with NAME into a project name, I just used konnichiwa(means “Good afternoon” in Japanese). I added a simple Konnichiwa class to the konnichiwa.rb file in the library directory. This class has a function hi, that prints “Good Afternoon!” to the screen.

4. Put a script in the bin directory that you can run. Read about how you can make a Ruby script that’s runnable for your system.
I followed this example to run ‘puts “Hello World”‘! Thanks to Commander Coriander.

5. Mention the bin script you created in your Gemspec so that it gets installed.
Under spec.executables, we add the name of the script we created in bin.

6. Use your Gemspec to install your own module and make sure it works, then use gem to uninstall it.

Built my very first Ruby gem!
Built my very first Ruby gem!
Installing and running the gem!
Installing and running the gem!
Uninstalling the gem, goodbye :(
Uninstalling the gem, goodbye 😦
After uninstalling, I can no longer use the gem.
After uninstalling, I can no longer use the gem.

Summary
Wow, this was a difficult exercise. Although I successfully made and installed my very first Ruby gem, the whole process was quite confusing. When I tried to uninstall it, it couldn’t find the path to the gem. (The gem was in the directory ruby-2.0.0-p247). I thought I could uninstall from the global gemset, but it said my PATH was not properly set up. It did say I could fix it temporarily in the session by saying use ruby-2.0.0-p247, which is how I successfully uninstalled the gem. I checked that the gem was no longer available by opening interactive ruby, and typed require ‘konnichiwa’. Yep, no such file!

This exercise was about creating a project skeleton with directories and files. This skeleton can be easily used in the future to start a project. A gemspec is a file that contains information about a Ruby gem and can be used to create a Ruby gem. We can make a Ruby script that’s runnable by the system by telling the computer we want it to run the script in a Ruby environment. Even though I still feel everything is ‘magical’, I’m going to give myself a pat on the back for making my first Ruby gem. Boom!

Last but not least, bin stands for binaries. Not garbage bin.

 

Advertisements

5 thoughts on “Learn Ruby the Hard Way – Exercise 46 A Project Skeleton

  1. Hey, so I’m finding two versions of ex46 for Ruby. One is what Shaw’s website says and the other is everyone’s examples or reviews doing it:
    http://www.mkltesthead.com/2012/01/exercise-46-project-skeleton-learn-ruby.html
    https://github.com/CreaturePhil/Learn-Ruby-the-Hard-Way/tree/master/ex46

    I’m just wondering which one you followed because I can’t for the life of me get the version on his site to work and when I try to google and find out why I get this other version which is then no help at all…

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s