Skip to content

Instantly share code, notes, and snippets.

@ryanbrunner
Created January 19, 2016 00:29
Show Gist options
  • Save ryanbrunner/8dfc6527972d0ed31237 to your computer and use it in GitHub Desktop.
Save ryanbrunner/8dfc6527972d0ed31237 to your computer and use it in GitHub Desktop.

Week 1, Day 2: Ruby and Git

Materials Required / Prep Work

  1. Make sure you have Git installed on your computer. If you don't, here's a guide to getting it setup - http://git-scm.com/downloads.
  2. If you haven't already, create an account on GitHub.
  3. Install the Heroku toolbelt, and set up an account on Heroku.

Opening Excercise

Let's try adding some functionality to our Sinatra App.

Basic

Try modifying your Sinatra app to return the following string:

The current date is (today's date)

Hints:

  • You can get today's date with Date.today
  • You can insert Ruby code into a string with string interpolation - "Hello #{person.name}"

Prostar

Try making your Sinatra app respond to a different URL.

Hints:

  • See the part that says get '/'? The slash is the part of the URL that goes after the domain name. See what happens if you change it.

Super Prostar

Try making your app have two different routes at the same time. One (/date) should return the current date, and the other (/day) should return the current day of the week.

Hints:

  • See what happens if you copy the whole get '/' do ... end setup.

Learning Goals / Objectives

Part 1 : Git

  • Introduction to Git.
  • Setting up a Git Repository and tracking changes.
  • Pushing your changes to GitHub

Part 2 : Objects, Classes and Methods in Ruby

  • Object-oriented programming in Ruby.
  • Methods, classes, and objects.
  • Creating your own Ruby classes and methods.

Part 3 : Heroku

  • Deploying to Heroku

Part 2 : Git

Git is a VCS (Version Control System). Think of Git as a history of every change you make in your application.


Reasons to use Git

  • Recover from mistakes - Git makes it easy to go back to a previous version if you make a mistake somewhere along the way.
  • Work with others - Git lets you share your source code with other developers, and lets multiple people make changes without getting in each others way.
  • Experiment - You can make branches of your code with Git, which lets you try out something new while keeping your place on code you know already works.

Let's set up a Git repository!

Setting up your Git repository is easy! Let's give it a try.

  1. Create a new folder called my-first-repo.
  2. cd into that directory.
  3. Type git init to initialize your repository. You should see the following:
Initialized empty Git repository in /Users/ryan/Code/ladieslearningcode/my-first-repo/.git/

Now that we have our Git repository created, let's add a file and track some changes.

  1. Create a file called hello.txt inside your my-first-repo. (Hint: You can quickly create a file on the command line by typing touch hello.txt)
  2. Type git status to see what git sees as the status of your repository. You should see something like this:
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
# hello.txt
nothing added to commit but untracked files present (use "git add" to track)

The important thing here is that hello.txt is listed under untracked files. Remember, Git won't keep track of your changes unless you tell it to. Let's tell git that we want to start tracking changes to this file.


  1. Type git add hello.txt to tell Git that you want to add hello.txt to git.
  2. Type git status again to see what git thinks about your directory.
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
# new file:   hello.txt
#

hello.txt is no longer under "Untracked files", it's now under "Changes to be committed". Files in this category are in the staging area of Git. We need to tell Git what this change is all about before we're done (commit changes).


  1. Type git commit -m "Adding hello.txt"
  2. Type git status one more time.
# On branch master
nothing to commit, working directory clean

Great! We've committed hello.txt to Git. But how can we make sure that it happened?


  1. Type git log to see a log of your changes.
commit e0303b5c090e0d657e5d0843f8fd579b3c7aaa9a
Author: Ryan Brunner <ryan@influitive.com>
Date:   Sun Feb 9 12:57:57 2014 -0500

    Adding hello.txt

git log shows a history of every change you've made to Git. Remember, in order to commit things to Git, its always a two stage process. You need to add the files you want (ie. new files or ones you've recently changed) and then commit them.


Let's try making a change to hello.txt to practice this some more.

  1. Open hello.txt in Sublime Text, and add some text to the file.
  2. Run git status again.
# On branch master
# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified:   hello.txt
#
no changes added to commit (use "git add" and/or "git commit -a")

hello.txt is no longer an untracked file, but Git still won't automatically commit changes to the file. We have to use the same process to add a file to git.


  1. We'll use a shortcut this time. Type git commit -am "Modified hello.txt". Including the -a in git commit automatically adds any files with changes.

Be careful about using git commit -am - you could inadvertendly add files you don't want.


Recap

  • git init - Create a new Git respository
  • git status - Show the current status of your repository
  • git add - Add changes or new files to Git
  • git log - Show the history of changes in a repository

Exercise:

Go back to your Sinatra application, initialize a Git repository, and add all the files you've created so far to your repository.

Pro-star: Try removing hello.txt from the my-first-repo repository. (Hint: Look into git rm to see how to do this.)

Super-pro-star: Create a new fille goodbye.txt with some content and commit it. Make some changes to goodbye.txt (without committing anything), and use git to undo all the changes. (Hint: Look carefully at the output of git status when you have changes to see what you need to do).


Pushing up to GitHub

Git hub isn't just a VCS, it's a DVCS (Distributed Version Control System). One of Git's strongest features is that you can have multiple copies of your repository on different computers or servers. You can make changes to all of these repositories and Git will help you work out how to combine these changes.

To add our repository on GitHub, we're first going to create a new repository on GitHub and then push our changes up there.

  1. Log into GitHub and click the plus button next to your name (in the upper right).
  2. Click on "New Repository".
  3. Enter "my-first-repo" as the repository name.

Great! Your GitHub repository is created. Now we need to get our changes up there. We already have an existing repository, so we'll follow the second set of GitHub instructions.


Enter the following in your command prompt (make sure you're in the my-first-repo directory).

git remote add origin git@github.com:ryanbrunner/my-first-repo.git
git push -u origin master

There's two new commands here:

  • git remote - Manages remote repositories
  • git push - Push all of the changes on your computer (that have been commited), to a remote repository.

Part 2 : Objects, Classes and Methods in Ruby

Let's review some key concepts in Ruby.

Objects

Everything in Ruby is an object. Numbers, strings, books, people - you name it. The most important thing to remember about objects is that they all respond to messages (called methods in Ruby). A method is a list of instructions on how to do a particular thing.

How do objects know what messages they respond to, and how to respond to them? They get that from their class. Think of a class as something that defines a type of object.


Let's use irb to learn a bit more about objects and their classes.

  1. Open up a Ruby console (by typing irb)
  2. Type 1.class into the console.
  3. Type "Hello World".class into the console.

You can use .class on any object in Ruby to determine its class. Try playing around with it, see if you can find some other examples.


Some examples of objects and their respective classes:

Object Class
11 Fixnum
"Hello" String
4.5 Float
Ford Focus Car
War of the Worlds Book

.class is just a method on Ruby. Methods are how you talk to objects in Ruby. Usually the objects will talk back, by returning a value. They might also change something about the object.

Every class has it's own set of messages that it knows about. .class is one method that every single class has, but others depend on the class of an object.


Some examples of methods (follow along in irb!)

Fixnum (numbers without decimals)

a = 1

a.even?
=> false

a.odd?
=> true

a.zero?
=> false

a.next
=> 2

Strings

a = "Hello world"

a.upcase
=> "HELLO WORLD"

a.reverse
=> "dlrow olleH"

a.length
=> 11

Methods with Arguments

Some methods take arguments:

a = 2

a.+ 5
=> 7

 # For some arithmetic symbols you don't need the dot:
a + 5
=> 7

a.between?(1,5)
=> true

For more methods, see the Ruby methods cheatsheet in your course notes.


Writing your own method

Let's try writing our own methods (in irb):

def hello
  "hi there"
end

hello
=> "hi there"

def add(a, b)
  a + b
end

add(1,2)
=> 3

You can put whatever Ruby code you want between def and end. Whatever line was executed last will be what the method returns.


Writing your own class

Let's write our first class. As you do bigger programs in irb, it's sometimes helpful to do everything in a text editor and then paste it in.

class Book
  attr_accessor :name, :author

  def headline
    "#{name} - #{author}"
  end
end

b = Book.new

b.name = "Harry Potter"
b.author = "J.K. Rowling"
b.headline
=> "Harry Potter - J.K. Rowling"

Let's look at a few things here:

  • Classes are defined by class <name>..end, just like a method.
  • attr_accessor defines a property of the class. Properties are like variables inside of a class that you can set to different values.
  • Classes can have methods - they work just like methods defined outside of classes. You can use properties of a class from methods inside a class.

Exercise

Try adding a price property to your Book class. Add the price into your headline method.

Pro-star

Try adding a cheaper? method that takes another Book as an argument, and returns true if that book is more expensive.

Super pro-star

Try adding a more_expensive? method that does the opposite. Try to use the cheaper? method you've already created, rather than re-implementing your logic.

Part 3 : Heroku

Heroku makes it really easy to push your work to the web (right now, it's stuck on your computer!) We'll learn more and more about Heroku as we build more complicated applications, but for now, let's get our application up.

  1. Get back to a command-line, and navigate to your Sinatra application.
  2. Type heroku create. You'll be asked for your Heroku credentials.
  3. Watch as Heroku pushes up your application.
  4. Once you're done, you should see something like the following:
Creating desolate-meadow-8947... done, stack is cedar
http://desolate-meadow-8947.herokuapp.com/ | git@heroku.com:desolate-meadow-8947.git
Git remote heroku added
  1. Try visiting the URL that Heroku gave you. If everything worked well, you should see your application!

How Heroku works

Heroku works by creating a remote git repository, just like GitHub. Whenever you push your changes to Heroku's repository, your web application will be updated.

By default, you're going to be pushing to the origin remote repository (GitHub). If you want to push to Heroku, type the following:

git push heroku

Note that you will need to make all your commits before you do that.


Homework

If you want to learn more about Git, including more complex operations like branching, GitHub has an excellent tutorial at Try Git

Here are some other great git resources that also might help

And for some fun Ruby practice, try and progress through The Ruby Warrior. A small prize for who can get the farthest.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment