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

ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

Embed Size (px)

Citation preview

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

WHO AM I & WHY AM I HERESTORY TIME!

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

PROGRAMMING 3-WAYTHE BATTLE BETWEEN GOOD AND EVIL?!

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.

DATING YOUR ORMEVERYONE IS DOING IT!

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?

THERE ARE OBJECTS EVERYWHERETHEY’RE IN MY RACCOON WOUNDS…

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

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

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?

CREEPNOT THE RADIOHEAD HIT SONG

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  

KEEPING IT OPEN“MONKEY PATCHING” IS AWESOME

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

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

SHARING IS CARING

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

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.

CONSOLE LOVE!I DIDN’T HAVE THIS BEFORE

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)

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

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

DEBUGGING THE UNKNOWN

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

QUESTIONS?

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 …