Skip to content

Instantly share code, notes, and snippets.

@Cspeisman
Last active August 29, 2015 14:11
Show Gist options
  • Save Cspeisman/50615cd2863201a1e5b5 to your computer and use it in GitHub Desktop.
Save Cspeisman/50615cd2863201a1e5b5 to your computer and use it in GitHub Desktop.

Welcome Intro to Ruby on Rails!


Hi! I'm...


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

--

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 nitrous.io

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

  • variables
  • methods
  • arrays
  • hashes

Variables


Methods

methods are a lot like variables

you save values to variable names

you save certain instructions to method names


Collections!

Arrays & Hashes


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('Corey')
person2 = Person.new('Pat')

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

--

Question 1 - What is Ruby on rails?

  • 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


Let's just jump into and build something cool!


Useful commands

  • rails new [name of app] creates a new rails app
  • rails generate scaffold [model name] [fields:datatype] generates the rails scaffold
  • rake db:migrate migrates over the database schema from ruby land to sql land
  • rails s starts the rails server
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment