Skip to content

Instantly share code, notes, and snippets.

@ryanbrunner
Created January 18, 2016 23:26
Show Gist options
  • Save ryanbrunner/362e29b0ff552ea37fa8 to your computer and use it in GitHub Desktop.
Save ryanbrunner/362e29b0ff552ea37fa8 to your computer and use it in GitHub Desktop.

Week 1, Day 1: Ruby, The Interwebs and YOU (but mainly Ruby & the interwebs)

Materials Required / Prep Work

  1. Opening a 'command-line' interpreter
  2. RVM (osx only), Ruby & Ruby-gems installed
  3. A Text Editor (Sublime Text strong recommended)
  4. Have the command line cheatsheet handy

Learning Goals / Objectives

Part 1 : The Command Line

  • Unix/Windows Command Line
  • Creating and Manipulating Files
  • Running commands
  • Loading Ruby Console
  • Installing Ruby gems

Part 2 : Ruby The Language

  • Understand Ruby (language) vs Rails/Sinatra (framework)
  • Creating and running ruby files
  • Installing ruby gems
  • Ruby REPL (irb)

Part 3 : Ruby Applications & The Web

  • Client-server & request-response pattern
  • Role of web/app server (e.g. WEBrick)
  • Server side language (e.g. Ruby)
  • Sinatra Web Framework
  • Serving static content

The Command Line

Developers spend a lot of their time working in the command line. Proficiency on the command line is the first step to proficiency as a programmer

It's important to understand a few distinctions:

  • Windows, OSX, Linux, Unix are all operating systems (OS)
  • All of these operating systems sit atop a File System that stores files
  • They all have a graphical UI that lets you manipulate files
  • The command line is the software operating in the background of the UI
  • Everything from the UI can be done from the command line such as
    • creating files, editing, renaming
    • running Ruby programs
    • sending email
    • pushing code to Github

Getting Started on the Command Line

Open up your 'terminal' application:

# OSX
Finder -> Applications -> Utilities -> Terminal
# OR
Use Spotlight (Command+Space) and search for 'Terminal'
# Windows
Start -> Run -> 'cmd'

The Prompt & Navigating

You're now shown a command prompt with a blinking cursor. Welcome to your new home!

We're going to move around your file system, the same way you might through 'Finder' or 'Windows Explorer'

# OSX
cd Desktop
# Windows
cd <Some Windows Directory>

Navigating

So 'cd' is just a 'command' that you're running. It stands for 'change directory'.

When you click a folder in the Explorer, you're saying 'cd' into that folder.

To go back you type cd ... Where '..' just means one level UP.

There are a few 'special' symbols like this:

  • . - Current Directory : cd . does nothing (change directory to current directory)
  • .. - One Directory Up : cd .. takes you one level up from current
  • ~ - Home Directory : cd ~ takes you home, no matter where you are

Faster Navigating

It would be tedious if we have to cd folder1, cd folder2 each time we wanted to change directories. In fact we can 'chain' these calls together to change multiple levels up or down. We do this be separating the directorys with a '/' (OSX), '' (Windows)

cd ../.. # go two levels up
cd Documents/Photos # go to Photos folder from your home directory
cd ~/Movies # go to your movies folder no matter where you are

Creating Folders

Folders should be used frequently for organizing your code to maintain your sanity.

To create a folder (ie directory) use mkdir (Make Directory)

mkdir folder_name

Spaces become annoying when you're navigating, so as a general rule, avoid them.


Creating Files

Files are what store your actualy code, they have a name and an extension. eg:

myfile.rb
# or
<file-name>.<extension>

Example extensions:

  • .doc (Microsoft Office Word file)
  • .rb (Ruby File)
  • .txt (Plain Text File)
  • .html (HTML File)

File names can be anything, but again generally without spaces. To create a file, simply: touch <filename>. ie:

touch cool_program.rb

Where Am I and What do I Contain

It might be nice to see where you currently are in your folder structure. To find out use pwd (Print Working Directory)

If you want to know what files exist in the current directory: ls (list)

If you want to see the contents of a files cat <filename> (catenate)


Exercises

  1. Make a new Folder to store all your code in. I usually do something like ~/Code/HackerYou
  2. Create your first project folder for today's work inside the above folder (ie w1d1)
  3. Add a new 'text' file to the project.
  4. Edit that file with your Text Editor and save
  5. Print out the contents of that file on the command line
  6. BONUS - rename that file to 'renamed.txt' (refer to cheatsheet)
  7. Bonus - create a copy of that file named 'copied.txt'

Ruby - The Language

Ruby is a programming language used for building applications. You might not have realized it, but all of the previous commands (ls etc...) were also using a language, but that one is called BASH (osx) or DOS Shell (windows)

So, Ruby is just like these guys, except it's more powerful and fun :)


Frameworks (Rails, Sinatra etc.)

A framework is just a collection of files that make every-day tasks easier.

Rails, for instance, is just a collection of ruby files that make writing web applications easier. It provides mechanisms for accepting requests from a browser, responding with content, saving data to a database and much, much more.


Ruby Command Line

Just like in BASH, we also have a separate Ruby command line that only speaks Ruby.

To run it, type irb into Terminal (Interactive Ruby)

You'll now see a different prompt like:

2.0.0 :001 >

You can exit back to your BASH prompt by typing exit + enter


Ruby Syntax

Just like the previous language (BASH) ruby has a particular syntax that it understands. If you write code outside of its allowed syntax, it won't be able to run the code. Try this in your bash console:

$ lz
bash: lz: command not found

So, just like bash above doesn't know what 'lz' is, ruby only lets you type certain commands at certain times.

To get a full understanding of what you can run in ruby, check out the full ruby api (Application Programming Interface)


Different than Bash

Let's go back into your IRB console. Try running one of your bash commands here:

2.0.0-p353 :008 > pwd
NameError: undefined local variable or method `pwd' for main:Object
  from (irb):8
  from /Users/bradrobertson/.rvm/rubies/ruby-2.0.0-p353/bin/irb:12:in `<main>'

This is the same as typing lz above in Bash. Ruby just doesn't know this command. Instead, try:

Dir.pwd
 => "/Users/bradrobertson/Code/hackeryou"

Get used to this!

This is a very important distinction and is probably one of the most confusing things for new programmers. Bash command line is different than Ruby command line and you NEED BOTH and you NEED TO UNDERSTAND both.

Don't worry, it's probably confusing right now, but it will become second nature shortly.


Running Ruby Files

The Ruby language uses .rb as its file extension. Anything you can type in your irb console, you can put in a ruby file.

Create a new file fun.rb and add the following

puts Dir.pwd

Now run that file using ruby fun.rb

Notice you get the same result (puts just means 'output' this result so you can see it)


One more important command

So, ruby is a programming language. Frameworks (and libraries) are just a collection of files (.rb files) written in the Ruby language. But how do we get these files?

This is where rubygems comes in. Rubygems allows you to package up a collection of files into a .gem file. Then, your application can reference these files and use the utilities they provide. You already have some ruby gems on your computer. To see them:

# bash
gem list

Installing / Removing Gems

Gems live on a remote server called rubygems.org. You can go and browse different gems that people have created to make your life easier. One day hopefully you'll be the author of a gem!

To Install a gem, just type gem install <gem name>.

gem install twitter  # Install the twitter gem (so we can tweet from our app)

Removing a gem:

gem uninstall twitter

Seeing all gems: gem list

Exercises

  1. Open a Ruby Console and play around. Try some commands like:
  • Time.now
  • "*" * 20
  • 3.even?
  • 3.odd?
  • puts "Hello, World"
  1. Create a new ruby file that, when run, outputs "I Love HackerYou!". Run the File
  2. Install the Twitter Gem
  3. Install the Rails gem
  4. Install the Sinatra Gem

Sinatra - Building Your First Ruby Webapp

Sinatra is a small 'framework' written in Ruby for processing web requests. It is a bare-bones offering that puts the developer in full controler. While this can be useful, it also requires more up-front knowledge than Rails about how Ruby works, how the web works etc. For this reason we will use it to showcase how ruby and the web works, but as we begin to write more complicated applications, we'll move over to Rails which will ultimately make your life a lot easier.


Using Sinatra

Resources

Check out the Sinatra docs. On the home page it shows you a one-file web app. It's really that simple. Let's create a new file like that an run it.

# my_first_sinatra.rb
require 'sinatra'

get '/' do
  "Hello World!"
end

Remember how we run a ruby file? Run it now. What's it telling you?

== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from WEBrick


Load up your webapp

That above says Sinatra is listening on 'port' 4567. That's not so important, except it's telling you where your webapp can be accessed. We're going to make a request to your app from the command line using a command called curl

Curl will make requests to any url you want. For this app we want to request localhost:4567

Open up a new terminal window (or tab) and type

curl localhost:4567

Mind Blown!!!


Sinatra Coolness

Ok maybe not, but still pretty cool. You've run your first Ruby webapp! Not too hard right?

You can create as many of these apps as you like. Eventually we'll be able to host them on the interwebs with Heroku.

Try changing "Hello World" to "Hello Earth" and make the same request. Why didn't that work? You need to restart your server. To do so, hit ctrl-c to stop, then restart (you know this)

Try again, you should see "Hello Earth" :)


Exercises

  • Run through this Sinatra Tutorial
  • For more advanced usage, check out the official Sinatra Intro
    • note There's a few concepts in here you don't know yet, they will be explained

Homework

Lots of practice with Ruby. Use the following resources:

  • Codecademy Ruby Track From our prep-work, if you haven't yet completed it. Can also be a great refresher.
  • Ruby Monk
  • Ruby Koans Slightly more advanced than Codecademy or Ruby Monk but worth it if you are up to the challenge.
  • Try to incorporate your learnings into a slightly cooler Sinatra app.
    • Create an app that prints out "The Time is: ", instead of "Hello World"
    • hint should actually be the current time and change each time you make a request
    • hint Current time is found with Time.now
    • hint You might need some String Interpolation or refer back to your Ruby Monk challenge from before
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment