Making a Ruby Gem

I made my very first Ruby gem today! Check it out here!

You can make a Ruby gem! Make an account with Rubygems.org and then follow the following steps. These steps assume you already have some code that works and you want to package it as a gem. It’s always good to keep copies of your code on Github or Bitbucket

  1. First check if your gem name has been taken by doing a search on Rubygems.org.
  2. Next, run this in your Terminal:
    $ bundle gem <gem-name>

    This will create the files you need in your gem package.

  3. Open up the gem-name.gemspec file and replace all the items that start with ‘TODO’ with your own information. Also replace the push host link to rubygems.org. Mine was located on line 19:
    spec.metadata['allowed_push_host'] = "https://rubygems.org"
  4. Go into your lib/gem-name/version.rb file and add your version number in this format: MAJOR.MINOR.PATCH
    MAJOR: for big changes to your gem
    MINOR: for small additions to your gem
    PATCH: bug fixes and such
    Unnamed QQ Screenshot20160106224046
  5. Create a new file inside your lib/gem-name/ folder named gem-name.rb and add your classes and methods here.
  6. Inside a different file located at lib/gem-name.rb you can make any module specifications. In this file, the first line should be
    require 'gem-name/gem-name' to pull in the file located at lib/gem-name/gem-name.rb
  7. Add, commit, and push to Git
  8. $ rake install

    This will build and install locally

    * $ rake build

    will build the gem locally

  9. $rake release

    build and push to Rugygems.org

  10. $ gem bump --to minor
    $ gem bump --to major
    $ gem bump --to 1.1.1

To add another owner to the gem, type this in your Terminal

$ gem owner gem-name --add <owner@email.com>

 

Advertisements

JavaScript String Manipulation Challenge

Going through Udacity‘s free online JavaScript course, I came across this challenge:

Your Challenge

If given a string of a two word name formatted with any mix of capitalization, can you manipulate the string to ensure the first name has a capital first letter and the last name is totally capitalized? Assume there’s a space between the names. For instance, turning a string like "cAmEROn PittMAN" into "Cameron PITTMAN". Your answer should be a single string saved to the variable called finalName.

Take a look at MDN’s documentation on string methods for clues, then try it yourself!

==============================================

My solution:

Unnamed QQ Screenshot20151001081020

The breakdown:

Line 1: var name = "AlbERt EINstEiN";

A variable called name has the value AlbErt EINstEiN. All lines in JS have to end with a semicolon so that the system knows it is the end of a line.

Line 3: function nameChanger(oldName) {

A function called nameChanger will take in an argument called oldName. All functions will start with a curly brace after the function name.

Line 4: var finalName = oldName;

A variable called finalName has the value of oldName.

Line 6:  var nameArray = oldName.split(' ');

The value of oldName is taken and a .split() method is called to make a string array from oldName. The array values are created using a ‘ ‘ (space) as the separator. For example, if
oldName = "AlbERt EINstEiN";
oldName.split(' ');
would equal have the values: [AlbERt', 'EINstEiN']

Then these values would be saved in a new variable called nameArray.

Line 7: var lastName = nameArray[1].toUpperCase();

The second value (since arrays start with 0, nameArray[1] would be the second value) in nameArray is changed to all uppercase letters. This is stored in a new variable called lastName. For example, if
nameArray = ['AlbERt', 'EINstEiN']
nameArray[0] = ['AlbERt']
nameArray[1] = ['EINstEiN']
nameArray[1].toUpperCase();
would be equal to ['EINSTEIN']
var lastName = ['EINSTEIN']

Line 8: var firstName = nameArray[0].toLowerCase().split('');

The first value in nameArray is changed to all lowercase letters. Then the letters will be separated from each other since the .split(”) is an empty string. So if
nameArray[0] = ['AlbERt'],
firstName = ['a', 'l', 'b', 'e', 'r', 't'];

Line 9: var firstLetter = firstName.splice(1).join('');

firstName will start the array from the second value (1) and then the array will be joined together to form one string. For example:
firstName = ['a', 'l', 'b', 'e', 'r', 't'];
firstName.splice(1) = ['l', 'b', 'e', 'r', 't'];
firstLetter = ['l', 'b', 'e', 'r', 't'].join('') = ['lbert'];

.splice() method
.join() method

Line 10: var firstNameJoin = firstName[0].toUpperCase() + firstLetter;

The first value in the array firstName is changed to uppercase, then added to the value of firstLetter.
firstName = ['a', 'l', 'b', 'e', 'r', 't'];
firstName[0] = ['a'];
firstName[0].toUpperCase() = ['A']
firstNameJoin = ['A'] + ['lbert'] = ['Albert'];

Line 12: finalName = firstNameJoin + ' ' + lastName;

finalName = [‘Albert’] + [‘ ‘] + [‘EINSTEIN’] = [‘Albert EINSTEIN’];

Line 14: return finalName;

Saves the value of finalName.

Line 18: console.log(nameChanger(name));

'console.log' will show on the screen whatever is in the parentheses. Inside the parentheses, the function we made, nameChanger, is called with a parameter inserted called ‘name‘. ‘name‘ is the name of the variable that was defined in the beginning on Line 1.

There are many other ways to find a workable solution, so don’t think you HAVE to do it this way.

Sandi Metz

Sandi Metz(http://www.sandimetz.com/) at the Atlanta RailsConf 2015 talking about object-oriented design: https://youtu.be/OMPfEXIlTVE

Looks at Ruby code, how to simplify and reduce the amount of code you write.

Sandi Metz wrote a book called Practical Object-Oriented Design in Ruby.
A description of what the book is all about and how it’s changed the way people learn to code:

Practical Object-Oriented Design in Ruby (POODR) is a programmers tale about how to write object-oriented code. It explains object-oriented design (OOD) using realistic, understandable examples.   POODR is a practical, readable introduction to how OOD can lower your costs and improve your applications.

POODR will help you:

  • Decide what belongs in a single class
  • Avoid entangling objects that should be kept separate
  • Define flexible interfaces among objects
  • Reduce programming overhead costs with duck typing
  • Successfully apply inheritance
  • Build objects via composition
  • Design cost-effective tests
  • Craft simple, straightforward, understandable code

If your code is killing you and the joy is gone, POODR has the cure.

Read more or purchase here: http://www.sandimetz.com/products/