Rails Is From Mars  Ruby Is From Venus Presentation 1
Upcoming SlideShare
Loading in...5

Rails Is From Mars Ruby Is From Venus Presentation 1






Total Views
Views on SlideShare
Embed Views



9 Embeds 254

http://axonflux.com 216
http://www.slideshare.net 15
http://bandd.pe 13
http://posterous.com 4
http://safe.txmblr.com 2
http://www.newsgator.com 1
http://www.axonflux.com 1
http://rnmp.tumblr.com 1
http://mini.bandd.co 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Rails Is From Mars  Ruby Is From Venus Presentation 1 Rails Is From Mars Ruby Is From Venus Presentation 1 Presentation Transcript

  • RAILS IS FROM MARS RUBY IS FROM VENUS Relationship Advice For Rails Developers
  • WHO AM I? • My name is Rein Henrichs. • That’s pronounced like “rain”. • But spelled differently. •I work at •I blog at reinh.com •I twitter @reinh •I like candlelit dinners and long walks on the beach.
  • insert logo here :( YOU KNOW RAILS
  • How many people here use Rails?
  • How many of you think you know Ruby as well as you know Rails?
  • How many of you have contributed to an open-source Ruby project?
  • How many of you have written your own gem?
  • How many of you would be comfortable writing an HTTP client library in Ruby?
  • How many of you could write your own web framework?
  • WHY SHOULD I LEARN RUBY? • It’s easy. • It’s fun. • It will make you better at Rails. • It will make you a better person.
  • HOW DO I LEARN RUBY? Some resources
  • Free stuff
  • WHY’S POIGNANT GUIDE It has cartoon foxes. Foxen? It has cartoon foxen.
  • PROGRAMMING RUBY a.k.a. The Pickaxe
  • MR. NEIGHBORLY’S HUMBLE LITTLE RUBY BOOK Pragmatically chunky bacon
  • Not so free stuff (but still really good)
  • RUBY IN A NUTSHELL Come on. Matz wrote it.
  • RUBY IN PRACTICE Look at that funny looking guy on the cover. At least it’s not a nasty monkey. Sorry, O’Reilly
  • RAILS HAS OPINIONS And Ruby Likes To Talk
  • Rails is opinionated software
  • Ruby is a communicative language
  • Sometimes, Ruby just wants someone to listen to it.
  • If you’re programming along, doing nicely, and all of a sudden your program gets balky, makes things hard for you, it’s talking. It’s telling you there is something important missing. – Kent Beck, Smalltalk Best Practice Patterns
  • It is your responsibility to listen to your code and be considerate of its needs.
  • Write Ruby code that communicates well but be respectful of Rails’ opinions
  • Other people who use your code (including six-months-later you) will thank you.
  • Ruby makes it easy to write simply, clearly and expressively
  • Rails has powerful idioms and conventions
  • Combining the two makes for a happy, fulfilling relationship
  • RUBY LOVES YOU But Sometimes You Drive Her Crazy
  • These are some of the things you do that drive Ruby crazy.
  • You’re welcome.
  • # Bad i = 0; while i < array.size do puts array[i] i += 1 end # Better for item in array puts item end # Best array.each do |item| puts item end
  • WHY? • Ruby has powerful iterators. • You don’t need to write your own. ... in ... just calls #each internally. • for
  • # Bad value = value ? value : quot;defaultquot; # Better value = value || quot;defaultquot; # Best value ||= quot;defaultquot;
  • WHY? • Ternaries (the ? : thing) are ugly. • Ruby has pretty assignment with operators like += and ||=
  • # Bad array << 42 unless array.include?(42) array = array + [42] unless array.include?(42) # Better array = array | [42] # Best array |= [42]
  • WHY? • Sometimes it just helps to know what set union is.
  • # Bad if value != nil && value != false # Good if value
  • WHY? • Ruby has a sane notion of truthiness
  • # Bad if value == 1 || value == 12 || value == 42 # Good if [1,12,42].include? value
  • WHY? • Brevity is not the goal • Readability is the goal • But if it is more readable and also shorter, go for it.
  • # Bad def request begin perform_request rescue RequestError => e log_error e end end # Good def request perform_request rescue RequestError => e log_error e end
  • WHY? • Method definitions are an implied begin block.
  • # Bad !!value # Good value
  • WHY? • Ruby does not not like clarity. • What you lose in readability you gain in nothing.
  • # Bad ActiveRecord::Base # Good ActiveRecord::Model
  • WHY? • Naming things is important. • Base? What does that even mean? • Sorry Rails, you got this one wrong. Better luck next time.
  • # Bad class PostsController < ApplicationController def recent Post.find :all, :conditions => ['posts.created_at > ?', 1.week.ago] end end
  • # Good class PostsController < ApplicationController def recent Post.within 1.week end end
  • class Post < ActiveRecord::Base named_scope :within, lambda {|seconds| :conditions => ['posts.created_at > ?', seconds.ago]} end
  • WHY? • Make your code more expressive • And more intention revealing. • In other words, say what you mean to say.
  • url_for(:blog, :posts, @post.id, :comments, :replies => true) # => http://example.com/blog/posts/19/comments?replies=true
  • # Bad def url_for(*args) root + args.map{|arg| parse_arg(arg)}.join('/'). gsub('/?', '?') end def parse_arg(arg) case arg when Array: arg.join('/') when Hash ret = [] each{|k,v| ret << quot;#{k}=#{v}quot;} ret = ret.join('&') '?' + ret else: arg.to_s end end
  • # Good def url_for(*args) root + args.to_params end
  • class Array def to_params map{|a| a.to_params}.join('/'). gsub('/?', '?') end end
  • # Array %w(foo bar bazz).to_params # quot;/foo/bar/bazzquot; # Hash {:foo => :bar}.to_params # quot;?foo=barquot;
  • WHY? • Ruby uses coercion in many places • 1.to_s • (1..10).to_a • Writingyour own coercion method can help you use Ruby’s ducktyping. • Separation of concerns.
  • RAILS PERFORMANCE Insert your “scaling” and “premature optimization” jokes here.
  • Yes, I went there.
  • Slow is only meaningful in comparison.
  • Ruby is slow? Compared to what?
  • Is your database slow?
  • Are your views slow?
  • Is your app server slow?
  • Are you using HTTP via carrier pigeon?
  • If you don’t know where the slow is, you’re not ready to optimize.
  • Don’t optimize prematurely, but don’t pessimize either.
  • Don’t write code you know will never, ever be fast.
  • # Really Bad (Optimally Pessimum) class Ballot < ActiveRecord::Base def <=>(other) votes.count <=> other.votes.count end end # Good (Potentially Optimum) class Ballot < ActiveRecord::Base # With a counter_cache on votes default_scope :order => :votes_count end
  • IN OTHER WORDS • Don’t worry about speed until you know where the slow is. • Worry about writing simply and expressively. • Well written code is easy to optimize for performance later. • Don’t write something you know will never, ever be fast.
  • IN CONCLUSION • Ruby is fun and easy (and friendly!). • Ruby will make you happy. • Be more thoughtful in the way you treat Ruby. • The more Ruby you know, the better you can become at Rails. • If you love Rails, you should love Ruby too. • Also, don’t be premature. No one likes that.
  • WHO AM I? • My name is Rein Henrichs. • That’s pronounced like “rain”. • But spelled differently. •I work at •I blog at reinh.com •I twitter @reinh •I like candlelit dinners and long walks on the beach.