- 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.
- If you haven't already, create an account on GitHub.
- Install the Heroku toolbelt, and set up an account on Heroku.
Let's try adding some functionality to our Sinatra App.
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}"
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.
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.
- Introduction to Git.
- Setting up a Git Repository and tracking changes.
- Pushing your changes to GitHub
- Object-oriented programming in Ruby.
- Methods, classes, and objects.
- Creating your own Ruby classes and methods.
- Deploying to Heroku
Git is a VCS (Version Control System). Think of Git as a history of every change you make in your application.
- 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.
Setting up your Git repository is easy! Let's give it a try.
- Create a new folder called
my-first-repo
. cd
into that directory.- 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.
- Create a file called
hello.txt
inside yourmy-first-repo
. (Hint: You can quickly create a file on the command line by typingtouch hello.txt
) - 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.
- Type
git add hello.txt
to tell Git that you want to addhello.txt
to git. - 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).
- Type
git commit -m "Adding hello.txt"
- 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?
- 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.
- Open
hello.txt
in Sublime Text, and add some text to the file. - 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.
- We'll use a shortcut this time. Type
git commit -am "Modified hello.txt"
. Including the-a
ingit commit
automatically adds any files with changes.
Be careful about using
git commit -am
- you could inadvertendly add files you don't want.
git init
- Create a new Git respositorygit status
- Show the current status of your repositorygit add
- Add changes or new files to Gitgit log
- Show the history of changes in a repository
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).
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.
- Log into GitHub and click the plus button next to your name (in the upper right).
- Click on "New Repository".
- 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 repositoriesgit push
- Push all of the changes on your computer (that have been commited), to a remote repository.
Let's review some key concepts in Ruby.
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.
- Open up a Ruby console (by typing
irb
) - Type
1.class
into the console. - 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!)
a = 1
a.even?
=> false
a.odd?
=> true
a.zero?
=> false
a.next
=> 2
a = "Hello world"
a.upcase
=> "HELLO WORLD"
a.reverse
=> "dlrow olleH"
a.length
=> 11
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.
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.
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.
Try adding a price
property to your Book class. Add the price into your headline method.
Try adding a cheaper?
method that takes another Book as an argument, and returns true if that book is more expensive.
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.
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.
- Get back to a command-line, and navigate to your Sinatra application.
- Type
heroku create
. You'll be asked for your Heroku credentials. - Watch as Heroku pushes up your application.
- 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
- Try visiting the URL that Heroku gave you. If everything worked well, you should see your application!
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.
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.