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.

Anthony Lewis

August 29, 2015
Tweet

More Decks by Anthony Lewis

Other Decks in Programming

Transcript

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

    of Rails Crash Course • @anthonylewis • [email protected] • http://anthonylewis.com
  2. Session Overview • What is Ruby? • What is Ruby

    on Rails? • Installation • Ruby Basics • Your First Rails App • Resources
  3. 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
  4. 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
  5. Bundler • Bundler manages an application's dependencies through its entire

    life across many machines systematically and repeatably. • http://gembundler.com
  6. 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
  7. 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
  8. 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
  9. What about an IDE? • Most Rails developers don’t use

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

    https://atom.io • Sublime Text 2 • http://www.sublimetext.com
  11. Mac OS X • Starting from scratch? • Use Rails

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

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

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

    • rails --version • 4 or greater
  15. Numbers & Math irb> 1 + 2 => 3 irb>

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

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

    "HiThere" irb> "Hi" * 3 => "HiHiHi"
  18. • Symbols are used as identifiers in Ruby • They

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

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

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

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

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

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

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

    def initialize(name = "World") @name = name end def say_hi puts "Hello, #{@name}" end end
  28. Create The App • Open a command prompt • Create

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

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

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

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

    it. • View • The User Interface • Controller • Glue that ties the UI to the Data.
  33. 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
  34. 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
  35. Learn Ruby Online • Try Ruby • http://tryruby.org • Ruby

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

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

    http://austinonrails.org • Austin.rb - 1st Mondays • http://austinrb.org
  38. Heroku • http://www.heroku.com • Simple Web App Deployment • Create

    a free account • Look over the Getting Started info