Slide 1

Slide 1 text

Intro to Ruby on Rails Anthony Lewis

Slide 2

Slide 2 text

Anthony Lewis • Engineering Team Lead - Sharethrough • Author of Rails Crash Course • @anthonylewis • [email protected] • http://anthonylewis.com

Slide 3

Slide 3 text

Session Overview • What is Ruby? • What is Ruby on Rails? • Installation • Ruby Basics • Your First Rails App • Resources

Slide 4

Slide 4 text

What is Ruby?

Slide 5

Slide 5 text

Ruby • A dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write. • Created by Yukihiro “Matz” Matsumoto • http://www.ruby-lang.org/en

Slide 6

Slide 6 text

MINASWAN Matz is nice and so we are nice.

Slide 7

Slide 7 text

Gem • RubyGems is the Ruby packaging system. It provides a standard format for distributing Ruby programs and libraries, and an easy to use tool for managing the installation of gem packages. • https://rubygems.org

Slide 8

Slide 8 text

Bundler • Bundler manages an application's dependencies through its entire life across many machines systematically and repeatably. • http://gembundler.com

Slide 9

Slide 9 text

Git • Git is a free & open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency. • http://git-scm.com • https://github.com

Slide 10

Slide 10 text

What is Ruby on Rails?

Slide 11

Slide 11 text

Rails • An open-source web framework that’s optimized for programmer happiness and sustainable productivity. It lets you write beautiful code by favoring convention over configuration. • Created by David Heinemeier Hanson • http://rubyonrails.org

Slide 12

Slide 12 text

Rails is Opinionated • Rails assumes there is a “best” way to do things and encourages that way. • It includes recommended gems by default • CoffeeScript and jQuery for JavaScript • SASS for CSS • MiniTest for Testing

Slide 13

Slide 13 text

What about an IDE? • Most Rails developers don’t use an IDE • RubyMine (IntelliJ IDEA for Ruby) • https://www.jetbrains.com/ruby/

Slide 14

Slide 14 text

Text Editor • The Classics: Emacs, Vim • Atom • https://atom.io • Sublime Text 2 • http://www.sublimetext.com

Slide 15

Slide 15 text

Installation

Slide 16

Slide 16 text

What You Need • Ruby 2+ • Rails 4+ • SQLite3 • Git 2+

Slide 17

Slide 17 text

Windows • Starting from scratch? • Use Rails Installer • http://railsinstaller.org

Slide 18

Slide 18 text

Mac OS X • Starting from scratch? • Use Rails Installer • http://railsinstaller.org • Use Homebrew? • brew install ruby • Try rbenv

Slide 19

Slide 19 text

Linux • Check your package system • Does it have Ruby 2.0 or later? • Enjoy building from source? • Try rbenv

Slide 20

Slide 20 text

rbenv • For Mac OS X and Linux • Install multiple versions of Ruby • Builds from source - dev tools required • https://github.com/sstephenson/rbenv

Slide 21

Slide 21 text

Ready To Go? • ruby --version • 2 or greater • rails --version • 4 or greater

Slide 22

Slide 22 text

Ruby Basics

Slide 23

Slide 23 text

Interactive Ruby • Open a Command Prompt or Terminal • Type irb and press Enter

Slide 24

Slide 24 text

Numbers & Math irb> 1 + 2 => 3 irb> 2 * 3 => 6 irb> 6 / 3 => 2

Slide 25

Slide 25 text

Floating Point irb> 6 / 3 => 2 irb> 7 / 3 => 2 irb> 7.0 / 3 => 2.33333333333333

Slide 26

Slide 26 text

Strings irb> "Hello" => "Hello" irb> "Hi" + "There" => "HiThere" irb> "Hi" * 3 => "HiHiHi"

Slide 27

Slide 27 text

String Interpolation irb> name = "Tony" => "Tony" irb> "Hi #{name}" => "Hi Tony"

Slide 28

Slide 28 text

• Symbols are used as identifiers in Ruby • They are like an immutable string • Use a single memory address Symbols irb> :hello => :hello

Slide 29

Slide 29 text

Constants • Name must start with an upper case • Typically written in ALL CAPS irb> PI = 3.14 => 3.14 irb> 2 * PI => 6.28

Slide 30

Slide 30 text

Arrays • A list of objects • Enclosed in square brackets irb> list = [1, 2, 3] => [1, 2, 3] irb> list[1] => 2

Slide 31

Slide 31 text

Hashes • A set of key value pairs • Enclosed in curly braces irb> person = {:name => "Tony"} => {:name=>"Tony"} irb> person[:name] => "Tony"

Slide 32

Slide 32 text

Ruby 1.9+ Syntax • Drop the hash rocket • Move the colon to the end of the symbol irb> person = {name: "Tony"} => {:name=>"Tony"} irb> person[:name] => "Tony"

Slide 33

Slide 33 text

Conditionals age = 21 if age < 13 puts "Child" elsif age < 18 puts "Teen" else puts "Adult" end

Slide 34

Slide 34 text

Boolean Logic • Any expression can be treated as a boolean • false and nil are considered false • All other values are true • Use the empty? method to see if a string or collection is empty

Slide 35

Slide 35 text

Iteration • See also the Enumerable module in the Ruby documentation list = [1, 2, 3, 4] list.each do |number| puts number end

Slide 36

Slide 36 text

Methods • A reusable block of code with a name def greet(name = "World") puts "Hello, #{name}" end irb> greet "Hello, World" => nil

Slide 37

Slide 37 text

Classes • A collection of data and methods class Greeter def initialize(name = "World") @name = name end def say_hi puts "Hello, #{@name}" end end

Slide 38

Slide 38 text

Your First Rails App

Slide 39

Slide 39 text

Create The App • Open a command prompt • Create a directory for your code: • mkdir code • cd code • Type this command: • rails new vinyl

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

View The App • Start the development server: • cd vinyl • bin/rails server • Open your web browser: • http://localhost:3000

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Rails Philosophy • Convention over configuration • Follow the Rails conventions and there will be very little configuration.

Slide 44

Slide 44 text

Rails Philosophy • DRY - Don’t Repeat Yourself • Specifying the same thing in more than one place leads to errors.

Slide 45

Slide 45 text

Rails Architecture • Model • Data and rules for manipulating it. • View • The User Interface • Controller • Glue that ties the UI to the Data.

Slide 46

Slide 46 text

Model View Controller Reading Data

Slide 47

Slide 47 text

Model View Controller Updating Data

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

Add Albums • Press Ctrl+C to stop the server • rails generate scaffold Album title year • bin/rake db:migrate • bin/rails server • http://localhost:3000/albums

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Rails Console • Open a new terminal tab • bin/rails console

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

Resources

Slide 55

Slide 55 text

Learn More Ruby • Programming Ruby 1.9 & 2.0 • The “PickAxe” Book • Dave Thomas, with Chad Fowler and Andy Hunt • The Ruby Way • Hal Fulton

Slide 56

Slide 56 text

Learn Ruby Online • Try Ruby • http://tryruby.org • Ruby Koans • http://rubykoans.com • Ruby has damaged your karma.

Slide 57

Slide 57 text

Learn More Rails • Ruby on Rails Tutorial • Michael Hartl • https://www.railstutorial.org/book/ • Rails Crash Course • Anthony Lewis

Slide 58

Slide 58 text

Local Community • Austin on Rails - 4th Tuesdays • http://austinonrails.org • Austin.rb - 1st Mondays • http://austinrb.org

Slide 59

Slide 59 text

GitHub • https://github.com • Social Coding • Create a free account • Read the GitHub Bootcamp

Slide 60

Slide 60 text

Heroku • http://www.heroku.com • Simple Web App Deployment • Create a free account • Look over the Getting Started info

Slide 61

Slide 61 text

Thank You!