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

Building products with ruby on rails

Building products with ruby on rails

Understanding what Ruby and Ruby on Rails are and how they are used.
Understanding why Ruby on Rails is the preferred and most popular web development framework.
Case studies, and a live demo, that explain how Ruby on Rails fits into the building of a web product.

Alex Notov

October 09, 2012
Tweet

More Decks by Alex Notov

Other Decks in Programming

Transcript

  1. Alex Notov General Assembly San Francisco 10/09/2012 BUILDING PRODUCTS WITH

    RUBY ON RAILS Wednesday, October 17, 12 Student Takeaways: -Understanding what Ruby and Ruby on Rails are and how they are used. -Understanding why Ruby on Rails is the preferred and most popular web development framework. -Case studies, and a live demo, that explain how Ruby on Rails fits into the building of a web product.
  2. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV 2 HELLO. General  Assembly  is  a  global  network  of  campuses  for   individuals  seeking  opportunity  and  educa7on  in   technology,  business,  and  design  founded  in  NYC. Follow  us  on  Twi@er  at  @GA_SF  for  the  latest  news  on   classes,  courses,  and  events  in  San  Francisco,  and  use  the   hashtag  #GASF  to  con7nue  the  conversa7on.   Take  a  look  at  our  course  catalog  at  generalassemb.ly/sf  for   a  list  of  classes  currently  offered. Wednesday, October 17, 12
  3. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV Cofounder  at Full  stack  developer Consultant Teacher Hi,  I  am  Alex 3 Wednesday, October 17, 12
  4. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV THE DUDE (& THE CO.) THAT STARTED IT ALL 4 Founded  by  Jason  Fried  in  1999 ...  who  hired  David  Heinemeier  Hansson  in  2004  to  build  ... Wednesday, October 17, 12 37signals  was  a  consul7ng  shop  and  they  wrote  everything  in  PHP   DHH  and  Jason  were  both  PHP  developers  ini7ally  and  met  through  the  PHP  community Basecamp  was  to  be  37signals  first  product
  5. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV WHY RUBY? 5 Created  by  Yukihiro  Matsumoto  (matz)  in  1995 Fun Freedom  (DHH) Beauty Power “Ruby  fits  my  brain  like  a  glove”  -­‐DHH  at  RubyConf,  New  Orleans  2010 “Ruby  is  designed  to  make  programmers  happy”  -­‐matz Wednesday, October 17, 12 APer  coding  in  PHP  for  a  long  7me,  he  decided  to  use  his  new  job  as  an  opportunity  to  learn  a  new  language   Coincidentally,  when  I  had  joined  BizeeBee  in  2010,  that  was  exactly  what  I  had  done.   Having  a  background  in  C#,  PHP,  and  Flash  Ac7onScript,  I  too  decided  that  it  was  perfect  7me  to  learn  a  new  language. DHH  decided  to  use  the  Ruby  programming  language  (created  in  1995  by  Yukihiro Matsumoto - Matz)  to  write  Basecamp   He  really  loved  Ruby  for  its  beauty  and  readability   “Ruby is, more than anything else, a language for writing beautiful code that makes programmers happy.” -http://www.linuxjournal.com/article/8686   He  started  “programming  in  anger”  in  Ruby  to  learn  the  language     While  he  advocates  this  technique  as  a  way  to  learn  a  programming  language,  it’s  not  necessarily  geared  at  complete   beginners  coming  to  programming  
  6. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV FUN & FREEDOM 6 Test Tinker Extend Contribute Wednesday, October 17, 12 I believe people want to express themselves when they program. They don't want to fight with the language. Programming languages must feel natural to programmers. I tried to make people enjoy programming and concentrate on the fun and creative part of programming when they use Ruby. The ruby community has a long standing culture of testing. Tinkering is encouraged. The full ruby source is available via github. The ruby language allows you to “reopen” and redefine the core functionality of the language. The GPL license encourages you to make your changes available to the general public. You may open pull requests against the ruby core. You should go forth and create. In a way, this is why the ruby community encourages one to “code in anger” (as DHH had put it). You learn by doing.
  7. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV BEAUTY & POWER 7 Wednesday, October 17, 12 Students:   Has  everyone  here  seen  HTML  before?   What  do  you  think  the  example  on  the  leP  does?   Why  do  you  think  the  example  on  the  leP  is  important? It  demonstrates  ruby’s  powerful  adaptability  for  problem  domains It  also  demonstrates  the  beauty  and  readability  of  the  language The  example  on  the  leP  is  wri@en  using  Markaby  (Markup  as  Ruby) Used  to  write  HTML  pages  in  pure  ruby In  this  case,  ruby  was  used  to  write  a  DSL  (domain  specific  language)  for  HTML. Interes_ngly  enough,  this  is  also  related  to  the  previous  slide...
  8. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV FREEDOM, REDUX 8 Jonathan  Gilleae Author  of  markaby  (and  lots  more) Known  as  _why why’s  (poignant)  Guide  to  Ruby Wednesday, October 17, 12
  9. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV OH AND JUST MORE THING... 9 “My  conscience  won’t  let  me  call  Ruby  a   computer  language.  That  would  imply  that   the  language  works  primarily  on  the computer’s  terms.  That  the  language  is   designed  to  accommodate  the  computer,   first  and  foremost.   That  therefore,  we,  the coders,  are  foreigners,  seeking  ci7zenship   in  the  computer’s  locale.  It’s  the   computer’s  language  and  we  are   translators  for  the world.” “We  can  no  longer  truthfully  call  it  a   computer  language.  It  is  coderspeak.  It  is   the  language  of  our  thoughts.” Wednesday, October 17, 12
  10. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV WHY RAILS? (ACCORDING TO DHH) 10 ...  the  generic  pieces   that  were  lec  acer  I   tried  to  use  Ruby  to   create  Basecamp  ... ...  the  collec_on  of  opinions  I  have  about  how  Web   applica_ons  should  be  constructed  ... Wednesday, October 17, 12 As  David  was  working  on  Basecamp,  he  found  that  he  was  wri7ng  a  lot  of  boilerplate  ruby  code.   So,  he  decided  to  start  encompassing  that  code  into  a  framework   Rails  was  born   “ It’s simply the generic pieces that were left after I tried to use Ruby to create Basecamp—the Web-based project management system from 37signals.” “From one point of view, it could be said to be the collection of opinions I have about how Web applications should be constructed... They aim to give most people most of what they want, most of the time.”
  11. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV NO, BUT REALLY, WHAT IS IT? 13 It  is  a  DSL  for   web  applica_ons! It  is  an  MVC   framework Wednesday, October 17, 12 Web  applica7ons Domain  specific  language Model  view  controller Framework
  12. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV MODEL VIEW CONTROLLER 14 pie  -­‐  view Give  me  pie Ok,  here’s  pie ingredients  -­‐  database pan  &  tools  -­‐  model baker  -­‐  controller Wednesday, October 17, 12
  13. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV WHY MVC? 15 SEPARATION  OF  CONCERNS MODULARIZATION Wednesday, October 17, 12
  14. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV DATABASE 16 Persistence Wednesday, October 17, 12 Storage  and  persistence  of  data
  15. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV MODEL 17 State Domain Wednesday, October 17, 12 The  role  of  the  model  is  to  answer  ques7ons  about  the  state  of  the  applica7on It  is  also  the  role  of  the  model  to  maintain  the  core  of  the  domain  logic
  16. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV CONTROLLER 18 Handle  requests Session Wednesday, October 17, 12 The controller’s role is to handle web requests and manage the session
  17. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV VIEW 19 UI Wednesday, October 17, 12 The  user  interface. What  the  user  sees. What  the  user  interacts  with.
  18. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV IN MORE [GEEKY] DEPTH 20 Wednesday, October 17, 12
  19. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV ACTIVERECORD - MODEL 21 Wednesday, October 17, 12
  20. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV ACTIONCONTROLLER - CONTROLLER 22 Wednesday, October 17, 12
  21. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV [SOME] COMPANIES THAT USE RAILS 23 Wednesday, October 17, 12 Some  of  the  biggest  names  in  consumer  web  use  RoR     Many  of  these  companies  started  out  as  Rails  only  shops   Evolved  their  technology  stacks  over  7me Rails  is  also  the  preferred  choice  for  many  startups This  is  just  the  7p  of  the  iceberg,  as  there  are  huge  companies  that  are  not  on  this  list   examples:  amazon,  electronic  arts,  ibm,  jp  morgan,  yahoo,  NYT,     they  use  ruby  and  ruby  on  rails  for  many  of  their  internal  applica7ons At  the  center  of  it  all:  37signals
  22. Wednesday, October 17, 12 Guess what that is? That’s the

    original sketch of twitter by Jack Dorsey from 2006
  23. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV A CASE STUDY: 25 Users Tweets Timeline Wednesday, October 17, 12
  24. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV BUILDING A TWITTER CLONE 26 As  a  user I  want  to  post  a  tweet In  order  to  share  what  I  am  up  to  with  my  followers As  a  user I  want  to  follow  another  user In  order  to  be  able  to  see  his  tweets As  a  user I  want  to  view  my  followed  users’  tweets In  order  to  know  what  they  are  up  to Wednesday, October 17, 12 Let  us  say  that  we  were  building  twi@er  from  scratch This  is  meant  to  be  a  thought  exercise. Disclaimer:  this  covers  about  50%  of  the  core  func7onality  of  twi@er. And  yet,  it  is  the  7p  of  the  iceberg  of  how  twi@er  actually  func7ons...
  25. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV IN TERMS OF ROLES AND ACTIONS 27 User  posts,  reads  Tweets User  follows  Users ...  on  a  _meline Wednesday, October 17, 12 Think  in  terms  of  roles  and  ac7ons The  roles  and  ac7ons  define  the  rela7onships  and  the  func7onality  we  would  need  to  build
  26. Let’s Code! Wednesday, October 17, 12 The controller’s role is

    to handle web requests (which are a user’s actions) So, let’s start there. Rails provides us with some awesome magic that gets us up and running very quickly... First, let’s make a new rails app. rails  new  .  -­‐-­‐git  (I’ve  already  done  this  step,  to  save  us  some  time  with   configuration) Now, let’s take a look at what happens when we view our new app in the browser. http://0.0.0.0:3000 So you’ll notice that rails has yet again decided to help us out... Let’s make a controller for tweets. Except we don’t have to write any actual code! Rails will generate a bunch of it for us. The actions will be “tweet” and “index” rails  generate  controller  Tweets  tweet  index Let’s take a look at what Rails generated for us and if everything works as expected. It seems that we don’t have anything changed. But, look at step 2 of the getting started: Set up a default route and remove public/index.html open config/routes.rb point out the routes generated for tweet and index.
  27. GENERAL ASSEMBLY 10/09/2012 BUILDING PRODUCTS WITH RUBY ON RAILS ALEX

    NOTOV RUBY PERFORMANCE AND SCALING THOUGHTS... 29 3000  tweets  per  second  on  average  in   2011  at  baseline  capacity 1/4  billion  tweets  per  day That  doesn’t  even  touch  web  requests Wednesday, October 17, 12 At  twi@er’s  size,  they  are  finding  that  they  are  reaching  ruby’s  performance  peaks. Twi@er  was  handling  about  3000  tweets  per  second  on  average  in  2011  at  baseline  capacity. That’s  roughly  1/4M  tweets  per  day. This  does  not  count  page  loads  and  other  types  of  requests. They  began  slowly  moving  away  from  ruby/rails  in  2011,  aPer  being  around  for  5  years.