-
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.
- What is programming? (briefly)
- Intro to ruby
- Intro to object-oriented programming
- Intro to rails
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.
In order to tell your computer how to do something, you must use a programming language.
... that's where ruby comes into play!
Ruby is a object oriented pogramming language that is simple, easy to learn, and elegant.
5.times do
puts "Hi, there!"
end
See?
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
We are going to be using Ruby REPL
Here are the code examples: Code along
puts "what is your name?"
name = gets.chomp
puts "Hi #{name}, how are you??"
-
Data Types
- Integer
- String
- Boolean
- Symbols
- Array
- Hash
-
Programming Basics
- Variables
- Methods
- Classes
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
Symbol = immutable values used often for keys in a hash
x = {}
x[:symbol] = 1
x[:symbol]
# => 1
x = 1
x # => 1
sung = "Engineer"
sung # => Engineer
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
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
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
- 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
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')
class ClassName
end
class definition starts with the keyword class followed by the class name and is delimited with end
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
@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
- Open Source Framework
- Built with and uses the ruby programming language
- Uses MVC framework (model, view. controller)
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!
- Download Postgresql for Heroku and Rails Postgresql
- Start your postgres server by running the GUI app
gem install rails
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
- 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