Skip to content

Instantly share code, notes, and snippets.

@sk187
Created July 20, 2015 20:55
Show Gist options
  • Save sk187/fa2920ed1cd0e1de0f28 to your computer and use it in GitHub Desktop.
Save sk187/fa2920ed1cd0e1de0f28 to your computer and use it in GitHub Desktop.

Welcome Intro to Ruby on Rails!

rails_image


Hi! I'm...

  • Sung Kim

  • I'm a software engineer & data scientist at Booz Allen Hamilton.

  • I'm @Quant_LIfe on Twitter but sk187 just about just everywhere else.


Agenda

  • What is programming? (briefly)
  • Intro to ruby
  • Intro to object-oriented programming
  • Intro to rails

What is programming?

When you tell someone to make a sandwich, your job is made much easier because they already know what a sandwich is.


If you were to tell your computer to make a sandwich you would have to give very specific directions.

  • open cabinet
  • see if there is bread
  • if there is no bread go to the store and buy bread
  • open bread packaging and take out 2 slices

Programming is telling your computer how to do something. Large tasks must be broken up into smaller tasks, which must be broken up into even smaller tasks, down until you get to the most basic tasks that you don't have to descibe - and that your computer already knows how to do.


Programming Languages

In order to tell your computer how to do something, you must use a programming language.


... that's where ruby comes into play!


What is Ruby?

Ruby is a object oriented pogramming language that is simple, easy to learn, and elegant.

	5.times do 
    	  puts "Hi, there!"
	end

See?


What is Ruby on Rails?

It is a framework built with ruby and uses ruby to build websites

  • Handles web requests
  • Easily configures to any database
  • makes best practice assumptions so that we don't have to

Getting started with ruby

We are going to be using Ruby REPL

Here are the code examples: Code along


Let's create our first program!

	puts "what is your name?"
	name = gets.chomp
	puts "Hi #{name}, how are you??"

Intro to Ruby

  • Data Types

    • Integer
    • String
    • Boolean
    • Symbols
    • Array
    • Hash
  • Programming Basics

    • Variables
    • Methods
    • Classes

Primative Data Types

Integer = some number

x = 1
x.class
# => Fixnum

x = 1.75
x.class
# => Float

String = letters

x = 'Sung'
x.class
# => String

Boolean = True or False

x = true
x.class
# => TrueClass 

Primative Data Types (continued)

Symbol = immutable values used often for keys in a hash

x = {}
x[:symbol] = 1 
x[:symbol]
# => 1 

Variables

x = 1
x # => 1

sung = "Engineer"
sung # => Engineer

Methods

Methods are a lot like variables. You save values to variable names like you save certain instructions to method names

def count(x)
	i = 1
	while i < x+1
		puts i
		i +=1
	end
end

count(5)
1
2
3
4
5
#=> nil

Collections: Arrays and Hash

Arrays are ordered lists of values starting at 0

array = ['a','b','c']
array[0]
# => a

Hashes are unordered key value pairs (Kind of like a dictionary)

hash = {a: 1, b: 2, c: 3}
hash.keys
# => [:a, :b, :c]
hash.values
# => [1, 2, 3]
hash[:a]
# => 1

Dictionary Sort

Instructions: Prompts user to enter a word

  • Tells user to enter another word or hit the return key
  • Repeats that process until the user hits the return key
  • Returns all the words entered in alphabetical order

Intro to Object Oriented

  • Objects are basic building blocks of Ruby
  • Literally everything is an object in Ruby
  • You can think of Objects as any noun (users, computers , animals, cars, pets, houses, etc..etc)

There are two steps to creating an object in ruby

The first is to define a class


Class

Classes are basically your blueprint or a template for your object

class Person
	def initialize(name)
		puts "A user is being created"
	end
end

A class can be used to create many objects


The second step is to instantiate the object (aka create your object from the class)

person1 = Person.new('Sung')
person2 = Person.new('Tesla')

Anatomy of a class

class ClassName
end

class definition starts with the keyword class followed by the class name and is delimited with end


Initialize method

class Box
	def initialize(w,h)
		@width = w
		@height = h
	end
end

The initialize method is considered our constructor. It is used when you want to initialize class variables at the same time as creating your class


Instance variables

@width = w
@height = h

instance variables are class attributes and become properties of the object when an object is instanciated. They can also be passed between all of the class methods


#Accessor methods

# define a class
class Box

	# constructor method
	def initialize(w,h)
		@width, @height = w, h
	end

	# accessor methods
	def printWidth
		@width
	end

	def printHeight
		@height
	end
end

# create an object
box = Box.new(10, 20)

# use accessor methods
x = box.printWidth
y = box.printHeight

Accessor methods allow us to access our instance variables from outside of the class


Intro to Rails (and websites in general)

websiteflow

Question 1 - What is Ruby on rails?

  • Open Source Framework
  • Built with and uses the ruby programming language
  • Uses MVC framework (model, view. controller) MVC

Let's talk a little bit about databases

  • Two "General Types" Sql and NoSql

###SQL -Excel like with rows and columns ###No SQL -some type of data store, like a JSON Object

	Person1 {
		name: 'Sung',
		job: ['Software Engineer', 'Instructor'],
		languages: ['Korean', 'English', 'French', 'Japanese'],
		planguages: ['Ruby', 'Python', 'Javascript']
	}

Let's just jump into and build something cool!

First

  • Download Postgresql for Heroku and Rails Postgresql
  • Start your postgres server by running the GUI app
gem install rails

Useful commands

rails new [name of app] -d postgresql #creates a new rails app
rails generate scaffold [model name] [field:datatype] 
	#generates the rails scaffold 

rake db:create #creates a new database 
rake db:migrate #migrates over the database schema from ruby land to sql land
rails s #starts the rails server

Deploying to Heroku

  • Download Homebrew for Git or install git by itself
-ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
homebrew git #to intall git

OR

  • Git Homepage
  • Create an account on Heroku
  • In your console pointed to the folder with your app,
git init # This starts Git 
git add . # This adds all files that have been changed
git commit -m "initial commit" # This saves all of the files you added with git add
gem install heroku # This installs the Heroku gem to so you can do Heroku CLI commands
heroku login # To pass authentication credentials 
heroku create # Adds a Heroku remote to your git
git remote -v # To check on all remotes for your git 
git push [remote name][branch name] # Pushes your local git code to a remote
git push heroku master # Publishes your local code to the Heroku Server
heroku open # Opens the website
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment