Top Banner
10 THINGS I LEARNED WHEN SWITCHING TO RUBY/RAILS DANIEL SMITH, @DANIELSMITH EVERFI
27

ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

Aug 02, 2015

Download

Software

danielrsmith
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

10 THINGS I LEARNED WHEN SWITCHING TO RUBY/RAILSDANIEL SMITH, @DANIELSMITH EVERFI

Page 2: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

WHO AM I & WHY AM I HERESTORY TIME!

Page 3: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

I APOLOGIZE FOR: IMAGES, FONTS, COLORS, LAYOUT ETC. …AND BAD JOKES

Page 4: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

PROGRAMMING 3-WAYTHE BATTLE BETWEEN GOOD AND EVIL?!

Page 5: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

1. RUBY VS. RAILSRuby != Rails

Command Ruby Rails s  =  “1-­‐1-­‐2012”    #=>  “1-­‐1-­‐2012”       #=>  “1-­‐1-­‐2012”    s.reverse              #=>  “2101-­‐1-­‐1”       #=>  “2101-­‐1-­‐1”  s.to_date              #=>  NoMethodError     #=>  Sun,  01  Jan  2012  

Common language/Framework issue: Objective-C + Cocoa C# + .NET

A lot of people don’t know the difference or what is part of what.

Page 6: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

DATING YOUR ORMEVERYONE IS DOING IT!

Page 7: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

2. KNOW YOUR ORMNot understanding ActiveRecord is like not knowing your best friends birthday. Spend time learning the relationships and constraints, etc. Understand migration paths and how they work. What does the model have to do with the database? How does a model influence other pieces?

Page 8: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

THERE ARE OBJECTS EVERYWHERETHEY’RE IN MY RACCOON WOUNDS…

Page 9: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

3. EVERYTHING IS AN OBJECTclass and module are in fact OBJECTS

What does this example return?     class  SomeClass;  end  

It’s just code being evaluated. What about this? Is this ok?

    3.times  do         class  SomeClass           ‘My  Class’         end       end

Page 10: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

DHH COMMANDS YOU!AT LEAST I FEEL LIKE HE IS JUDGING ME…

Page 11: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

4. THE RUBY WAY/RAILS WAYDon’t port your old habits over - learn new ones.

Syntax Ruby should read like English (well sort of) White space matters…semicolons not so much USE BLOCKS Odd numbers: (1..100).step(2)  

Conditionals: do  something  if  something?

Page 12: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

CREEPNOT THE RADIOHEAD HIT SONG

Page 13: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

5. SCOPE CREEP IS REALUnderstanding variable scope can be important @someVar VS. someVar  

Making good choices in passing variables to view layer Partials <%  render  somevar:  @someThing  %>  

Avoid spaghetti code! Avoid shadow variables

people  =  [‘Andrew’,  ‘Bill’,  ‘Calvin’]  person  =  ‘Daniel’  people.each  do  |person|     puts  “It’s  #{person}’s  turn!”  end  

Page 14: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

KEEPING IT OPEN“MONKEY PATCHING” IS AWESOME

Page 15: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

6. THROWING POO (MONKEYPATCHING)“Monkey patching is awesome as long as you are the one doing it” - @derekgallo I love open classes!!! Be careful! UNIT TEST!!! Understand what your gems monkey patch Using open classes for organization - string.rb in ActiveSupport

Page 16: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

STRING.RBrequire  'active_support/core_ext/string/conversions'  require  'active_support/core_ext/string/filters'  require  'active_support/core_ext/string/multibyte'  require  'active_support/core_ext/string/starts_ends_with'  require  'active_support/core_ext/string/inflections'  require  'active_support/core_ext/string/access'  require  'active_support/core_ext/string/xchar'  require  'active_support/core_ext/string/behavior'  require  'active_support/core_ext/string/interpolation'  require  'active_support/core_ext/string/output_safety'  require  'active_support/core_ext/string/exclude'  require  'active_support/core_ext/string/encoding'  require  'active_support/core_ext/string/strip'  require  'active_support/core_ext/string/inquiry'  

All of which start with: class  String

Page 17: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

SHARING IS CARING

Page 18: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

7. WHAT DOES THAT GEM DO?!Lets play a game and guess what each of these do!

gem oink gem dalli gem wolverine gem kaminari

Page 19: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

7. GEM ABUSEAsk yourself these questions when adding a gem:

What does it really do? Do we really need it? Do we have another gem that already does this? Is it well maintained? (check the repo!) Is there a better alternative?

Think about what it might change about your application.

Page 20: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

CONSOLE LOVE!I DIDN’T HAVE THIS BEFORE

Page 21: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

8. IRB AND THE RAILS CONSOLEStopped using (for the most part) a database tool Run most lookups/inserts in a console

Makes sure you are using model’s logic Helps debug code Quickly test snippets (often, even when writing this)

Page 22: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

I <3 λ BLOCKS, PROCS, LAMBDAS OH MY!

Page 23: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

9. BLOCKS RUN THE WORLDLambdas have a return - procs do not. Return differences

Lambda returns from itself (like an anonymous function) Proc returns from its “parent” method

def  some_proc     myProc  =  Proc.new  {  return  }     myProc.call     puts  “After  the  proc”  end  !some_proc

def  some_lambda     myLbda  =  lambda  {  return  }     myLbda.call     puts  “After  the  lambda”  end  !some_lambda

Page 24: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

DEBUGGING THE UNKNOWN

Page 25: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

10. DEBUGGING THE UNKNOWNNoMethodError, method_missing() to the rescue

define_method is awesome

Dynamic methods/attributes in ActiveRecord Attributes are defined using #method_missing() SHOCKER Rails 4.0 kills off find_by* :-)

Routing methods (use them) _url _path

Page 26: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

QUESTIONS?

Page 27: ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

ADDITIONAL INFO

Daniel Smith Senior Software Architect - EverFi @danielrsmith - Twitter http://danielrs.com - The Internets

RubyMine Dash Alfred awesome_print zsh oh_my_zsh HomeBrew Atom.io CodeSchool HoneyBadger NewRelic …