ORUG - Sept 2014 - Lesson When Learning Ruby/Rails

Publish in

Software

6 views

Please download to get full document.

View again

of 27
All materials on our website are shared by users. If you have any questions about copyright issues, please report us to resolve them. We are always happy to assist you.
Share
Description
1. 10 THINGS I LEARNED WHEN SWITCHING TO RUBY/RAILS DANIEL SMITH, @DANIELSMITH EVERFI 2. WHO AM I & WHY AM I HERE STORY TIME! 3. I APOLOGIZE FOR: IMAGES, FONTS,…
Transcript
  • 1. 10 THINGS I LEARNED WHEN SWITCHING TO RUBY/RAILS DANIEL SMITH, @DANIELSMITH EVERFI
  • 2. WHO AM I & WHY AM I HERE STORY TIME!
  • 3. I APOLOGIZE FOR: IMAGES, FONTS, COLORS, LAYOUT ETC. …AND BAD JOKES
  • 4. PROGRAMMING 3-WAY THE BATTLE BETWEEN GOOD AND EVIL?!
  • 5. 1. RUBY VS. RAILS Ruby != 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.
  • 6. DATING YOUR ORM EVERYONE IS DOING IT!
  • 7. 2. KNOW YOUR ORM Not 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?
  • 8. THERE ARE OBJECTS EVERYWHERE THEY’RE IN MY RACCOON WOUNDS…
  • 9. 3. EVERYTHING IS AN OBJECT class 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
  • 10. DHH COMMANDS YOU! AT LEAST I FEEL LIKE HE IS JUDGING ME…
  • 11. 4. THE RUBY WAY/RAILS WAY Don’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?
  • 12. CREEP NOT THE RADIOHEAD HIT SONG
  • 13. 5. SCOPE CREEP IS REAL Understanding 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
  • 14. KEEPING IT OPEN “MONKEY PATCHING” IS AWESOME
  • 15. 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
  • 16. STRING.RB require '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
  • 17. SHARING IS CARING
  • 18. 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
  • 19. 7. GEM ABUSE Ask 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.
  • 20. CONSOLE LOVE! I DIDN’T HAVE THIS BEFORE
  • 21. 8. IRB AND THE RAILS CONSOLE Stopped 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)
  • 22. I <3 λ BLOCKS, PROCS, LAMBDAS OH MY!
  • 23. 9. BLOCKS RUN THE WORLD Lambdas 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
  • 24. DEBUGGING THE UNKNOWN
  • 25. 10. DEBUGGING THE UNKNOWN NoMethodError, 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
  • 26. QUESTIONS?
  • 27. 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 …
  • Related Search

    Next Document

    WHS AP Psychology

    We Need Your Support
    Thank you for visiting our website and your interest in our free products and services. We are nonprofit website to share and download documents. To the running of this website, we need your help to support us.

    Thanks to everyone for your continued support.

    No, Thanks