rails is from mars ruby is from venus presentation 1
Post on 12-Sep-2014
4.224 Views
Preview:
DESCRIPTION
TRANSCRIPT
RAILS IS FROM MARSRUBY 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.
YOU KNOW RAILS
insert logohere
:(
BUT DO YOU KNOW RUBY?
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 GUIDEIt has cartoon foxes. Foxen? It has cartoon foxen.
PROGRAMMING RUBYa.k.a. The Pickaxe
MR. NEIGHBORLY’S HUMBLE LITTLE
RUBY BOOKPragmatically 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
THE RUBY WAY
RUBY FOR RAILS
THE WELL-GROUNDED
RUBYIST
RAILS HAS OPINIONSAnd Ruby Likes To Talk
Rails is opinionated software
Ruby is a communicative language
Sometimes, Ruby just wantssomeone 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 YOUBut Sometimes You Drive Her Crazy
These are some of the things you do that drive Ruby crazy.
You’re welcome.
# Badi = 0; while i < array.size do puts array[i] i += 1end
# Betterfor item in array puts itemend
# Bestarray.each do |item| puts itemend
WHY?
• Ruby has powerful iterators.
• You don’t need to write your own.
• for ... in ... just calls #each internally.
# Badvalue = value ? value : "default"
# Bettervalue = value || "default"
# Bestvalue ||= "default"
WHY?
• Ternaries (the ? : thing) are ugly.
• Ruby has pretty assignment with operators like += and ||=
# Badarray << 42 unless array.include?(42)array = array + [42] unless array.include?(42)
# Betterarray = array | [42]
# Bestarray |= [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 << "#{k}=#{v}"}
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 # "/foo/bar/bazz"
# Hash
{:foo => :bar}.to_params # "?foo=bar"
WHY?
• Ruby uses coercion in many places
• 1.to_s
• (1..10).to_a
• Writing your own coercion method can help you use Ruby’s ducktyping.
• Separation of concerns.
RAILS PERFORMANCEInsert your “scaling” and “premature optimization” jokes here.
Yes, I went there.
Slow is only meaningfulin 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.
top related