Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Intro to Ruby on Rails

Intro to Ruby on Rails

Presented at Back to School with General Assembly on August 29, 2015.

3311968fbb383cc2111af84f36508d93?s=128

Anthony Lewis

August 29, 2015
Tweet

Transcript

  1. Intro to Ruby on Rails Anthony Lewis

  2. Anthony Lewis • Engineering Team Lead - Sharethrough • Author

    of Rails Crash Course • @anthonylewis • me@anthonylewis.com • http://anthonylewis.com
  3. Session Overview • What is Ruby? • What is Ruby

    on Rails? • Installation • Ruby Basics • Your First Rails App • Resources
  4. What is Ruby?

  5. 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
  6. MINASWAN Matz is nice and so we are nice.

  7. 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
  8. Bundler • Bundler manages an application's dependencies through its entire

    life across many machines systematically and repeatably. • http://gembundler.com
  9. 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
  10. What is Ruby on Rails?

  11. 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
  12. 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
  13. What about an IDE? • Most Rails developers don’t use

    an IDE • RubyMine (IntelliJ IDEA for Ruby) • https://www.jetbrains.com/ruby/
  14. Text Editor • The Classics: Emacs, Vim • Atom •

    https://atom.io • Sublime Text 2 • http://www.sublimetext.com
  15. Installation

  16. What You Need • Ruby 2+ • Rails 4+ •

    SQLite3 • Git 2+
  17. Windows • Starting from scratch? • Use Rails Installer •

    http://railsinstaller.org
  18. Mac OS X • Starting from scratch? • Use Rails

    Installer • http://railsinstaller.org • Use Homebrew? • brew install ruby • Try rbenv
  19. Linux • Check your package system • Does it have

    Ruby 2.0 or later? • Enjoy building from source? • Try rbenv
  20. rbenv • For Mac OS X and Linux • Install

    multiple versions of Ruby • Builds from source - dev tools required • https://github.com/sstephenson/rbenv
  21. Ready To Go? • ruby --version • 2 or greater

    • rails --version • 4 or greater
  22. Ruby Basics

  23. Interactive Ruby • Open a Command Prompt or Terminal •

    Type irb and press Enter
  24. Numbers & Math irb> 1 + 2 => 3 irb>

    2 * 3 => 6 irb> 6 / 3 => 2
  25. Floating Point irb> 6 / 3 => 2 irb> 7

    / 3 => 2 irb> 7.0 / 3 => 2.33333333333333
  26. Strings irb> "Hello" => "Hello" irb> "Hi" + "There" =>

    "HiThere" irb> "Hi" * 3 => "HiHiHi"
  27. String Interpolation irb> name = "Tony" => "Tony" irb> "Hi

    #{name}" => "Hi Tony"
  28. • Symbols are used as identifiers in Ruby • They

    are like an immutable string • Use a single memory address Symbols irb> :hello => :hello
  29. Constants • Name must start with an upper case •

    Typically written in ALL CAPS irb> PI = 3.14 => 3.14 irb> 2 * PI => 6.28
  30. Arrays • A list of objects • Enclosed in square

    brackets irb> list = [1, 2, 3] => [1, 2, 3] irb> list[1] => 2
  31. Hashes • A set of key value pairs • Enclosed

    in curly braces irb> person = {:name => "Tony"} => {:name=>"Tony"} irb> person[:name] => "Tony"
  32. 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"
  33. Conditionals age = 21 if age < 13 puts "Child"

    elsif age < 18 puts "Teen" else puts "Adult" end
  34. 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
  35. Iteration • See also the Enumerable module in the Ruby

    documentation list = [1, 2, 3, 4] list.each do |number| puts number end
  36. Methods • A reusable block of code with a name

    def greet(name = "World") puts "Hello, #{name}" end irb> greet "Hello, World" => nil
  37. Classes • A collection of data and methods class Greeter

    def initialize(name = "World") @name = name end def say_hi puts "Hello, #{@name}" end end
  38. Your First Rails App

  39. Create The App • Open a command prompt • Create

    a directory for your code: • mkdir code • cd code • Type this command: • rails new vinyl
  40. None
  41. View The App • Start the development server: • cd

    vinyl • bin/rails server • Open your web browser: • http://localhost:3000
  42. None
  43. Rails Philosophy • Convention over configuration • Follow the Rails

    conventions and there will be very little configuration.
  44. Rails Philosophy • DRY - Don’t Repeat Yourself • Specifying

    the same thing in more than one place leads to errors.
  45. Rails Architecture • Model • Data and rules for manipulating

    it. • View • The User Interface • Controller • Glue that ties the UI to the Data.
  46. Model View Controller Reading Data

  47. Model View Controller Updating Data

  48. None
  49. 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
  50. None
  51. None
  52. Rails Console • Open a new terminal tab • bin/rails

    console
  53. None
  54. Resources

  55. 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
  56. Learn Ruby Online • Try Ruby • http://tryruby.org • Ruby

    Koans • http://rubykoans.com • Ruby has damaged your karma.
  57. Learn More Rails • Ruby on Rails Tutorial • Michael

    Hartl • https://www.railstutorial.org/book/ • Rails Crash Course • Anthony Lewis
  58. Local Community • Austin on Rails - 4th Tuesdays •

    http://austinonrails.org • Austin.rb - 1st Mondays • http://austinrb.org
  59. GitHub • https://github.com • Social Coding • Create a free

    account • Read the GitHub Bootcamp
  60. Heroku • http://www.heroku.com • Simple Web App Deployment • Create

    a free account • Look over the Getting Started info
  61. Thank You!