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

Riding Rails

Riding Rails

Brief introduction to Ruby and Rails for a workshop that took place during CodeWeek @ DI in University of Minho

6497e10d8345ce6fee06048127196d6b?s=128

Fernando Mendes

October 16, 2014
Tweet

Transcript

  1. Riding Rails Code Week @ DI

  2. Miguel Pinto Fernando Mendes twitter: fribmendes github: frmendes github: miguelpinto98

    skype: miguelpinto.98
  3. None
  4. gem install rails If you have Ruby installed: To install

    Ruby on OS X: 1. Install Homebrew 2. brew install rbenv ruby-build To install Ruby on Linux: \curl -sSL https://get.rvm.io | bash -s stable --ruby --rails
  5. What I will be talking about 1. Ruby syntax 2.

    Basic Rails What I won’t be talking about: 1. Design 2. Patterns 3. Good practices Basic concepts = slides Everything else = examples
  6. Rails is Ruby

  7. Ruby is NOT Java Object oriented Testable Programmer happiness Everything

    is an object “So beautiful, tears are shed” _why, the lucky stiff
  8. Meet Matz Matz Not Matz

  9. Hello world! Ruby is all about idiomatic code We do

    not require parentheses
  10. Duck typing “If it walks like a duck, swims like

    a duck and quacks like a duck, it’s probably a duck.”
  11. Idiomatic ifs No curly brackets! No annoying ; One liners!

    Easy to read!
  12. Idiomatic ifs btw, this is our string interpolation

  13. Idiomatic ifs You can put this in the one- liner

    syntax
  14. Methods

  15. Methods Wait a second, where are we returning the values!?

  16. Methods Returns the last interpreted expression Ruby is smart Default

    values
  17. Classes Creates new instance with Elevator.new(weight, capacity) Translates to

  18. Classes Operator override just feels more natural. If you don’t

    like it, we also have Object#eql? and Object#equal?
  19. Classes Also, surprise surprise! This code has a trap. Can

    you find it?
  20. Classes Remember that the last evaluated expression is the one

    that gets returned?
  21. Classes Only this is going to get returned. If we

    wanted to write the correct method we would have to use the && operator.
  22. Classes public: default value, accessed by anyone protected: method can

    be accessed by the class or subclasses private: method can only be accessed by the class
  23. Classes class variable class method

  24. Blocks and High Order Functions Multi-line syntax One-line syntax

  25. Blocks and High Order Functions x is unchanged

  26. Blocks and High Order Functions x is unchanged x is

    changed Ruby convention: ! ending methods change the object ? ending methods return a boolean
  27. Symbols Strings assign different objects

  28. Symbols Symbols are always the same

  29. Wat the wtf is MVC

  30. Short answer:

  31. Not so short answer:

  32. My first app

  33. rails new project

  34. /app ..../controller ..../helpers ..../models ..../views ....../layouts /bin /config /db /doc

    /lib /log /public /test /tmp /vendor
  35. External libraries (gems) are imported into your project. Can have

    specified versions, minimum version, etc. Can Should be divided into groups Not necessarily Rails-exclusive Gemfile
  36. Gemfile bundle install

  37. Bringing up the server rails server Check it for yourself:

    go to localhost:3000
  38. Checking the console rails console

  39. rails generate scaffold User username:string name:string

  40. Scaffolding rails generate scaffold User username:string name:string model name

  41. Scaffolding rails generate scaffold User username:string name:string attribute name

  42. Scaffolding rails generate scaffold User username:string name:string attribute type

  43. Views Under app/views/<controller_name>/<controller_method>

  44. Views Under app/views/<controller_name>/<controller_method> By default, Rails uses embedded Ruby .html.erb

  45. Views Under app/views/<controller_name>/<controller_method> By default, Rails uses embedded Ruby .html.erb

    Have access to the instance variables created by the controller.
  46. Into the CRUD Create Read Update Delete

  47. Models User.new(username: “frmendes”, name: “Fernando Mendes”)

  48. User.all User.count Models

  49. User.new(username: “frmendes”, name: “Fernando Mendes”).save Models

  50. User.create(username: “codeweek”, name: “at DI”) Models

  51. User.find(1) User.find_by(username: “frmendes”) Models

  52. Controllers and Routes

  53. Migrations

  54. Migrations rails generate migration AddIndexToUsers

  55. Validations

  56. Ruby Make rake db:{drop,migrate} You better be f*cking careful!!1!

  57. Relationships

  58. Miguel Pinto Fernando Mendes twitter: fribmendes github: frmendes github: miguelpinto98

    skype: miguelpinto.98