rails is from mars ruby is from venus presentation 1
Post on 12-Sep-2014
4.224 views
DESCRIPTION
TRANSCRIPT
![Page 1: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/1.jpg)
RAILS IS FROM MARSRUBY IS FROM VENUS
Relationship Advice For Rails Developers
![Page 2: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/2.jpg)
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.
![Page 3: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/3.jpg)
YOU KNOW RAILS
insert logohere
:(
![Page 4: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/4.jpg)
BUT DO YOU KNOW RUBY?
![Page 5: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/5.jpg)
How many people here use Rails?
![Page 6: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/6.jpg)
How many of you think you know Ruby as well as you know Rails?
![Page 7: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/7.jpg)
How many of you have contributed to an open-source
Ruby project?
![Page 8: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/8.jpg)
How many of you have written your own gem?
![Page 9: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/9.jpg)
How many of you would be comfortable writing an HTTP
client library in Ruby?
![Page 10: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/10.jpg)
How many of you could write your own web framework?
![Page 11: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/11.jpg)
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.
![Page 12: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/12.jpg)
HOW DO I LEARN RUBY?Some resources
![Page 13: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/13.jpg)
Free stuff
![Page 14: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/14.jpg)
WHY’S POIGNANT GUIDEIt has cartoon foxes. Foxen? It has cartoon foxen.
![Page 15: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/15.jpg)
PROGRAMMING RUBYa.k.a. The Pickaxe
![Page 16: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/16.jpg)
MR. NEIGHBORLY’S HUMBLE LITTLE
RUBY BOOKPragmatically chunky bacon
![Page 17: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/17.jpg)
Not so free stuff (but still really good)
![Page 18: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/18.jpg)
RUBY IN A NUTSHELL
Come on. Matz wrote it.
![Page 19: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/19.jpg)
RUBY IN PRACTICE
Look at that funny looking guy on the cover. At least it’s not a nasty monkey. Sorry, O’Reilly
![Page 20: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/20.jpg)
THE RUBY WAY
![Page 21: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/21.jpg)
RUBY FOR RAILS
![Page 22: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/22.jpg)
THE WELL-GROUNDED
RUBYIST
![Page 23: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/23.jpg)
RAILS HAS OPINIONSAnd Ruby Likes To Talk
![Page 24: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/24.jpg)
Rails is opinionated software
![Page 25: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/25.jpg)
Ruby is a communicative language
![Page 26: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/26.jpg)
Sometimes, Ruby just wantssomeone to listen to it.
![Page 27: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/27.jpg)
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
![Page 28: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/28.jpg)
It is your responsibility to listen to your code and be considerate of its needs.
![Page 29: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/29.jpg)
Write Ruby code that communicates well but be respectful of Rails’ opinions
![Page 30: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/30.jpg)
Other people who use your code (including six-months-later you)
will thank you.
![Page 31: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/31.jpg)
Ruby makes it easy to write simply, clearly and expressively
![Page 32: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/32.jpg)
Rails has powerful idioms and conventions
![Page 33: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/33.jpg)
Combining the two makes for a happy, fulfilling relationship
![Page 34: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/34.jpg)
RUBY LOVES YOUBut Sometimes You Drive Her Crazy
![Page 35: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/35.jpg)
These are some of the things you do that drive Ruby crazy.
![Page 36: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/36.jpg)
You’re welcome.
![Page 37: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/37.jpg)
# Badi = 0; while i < array.size do puts array[i] i += 1end
# Betterfor item in array puts itemend
# Bestarray.each do |item| puts itemend
![Page 38: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/38.jpg)
WHY?
• Ruby has powerful iterators.
• You don’t need to write your own.
• for ... in ... just calls #each internally.
![Page 39: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/39.jpg)
# Badvalue = value ? value : "default"
# Bettervalue = value || "default"
# Bestvalue ||= "default"
![Page 40: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/40.jpg)
WHY?
• Ternaries (the ? : thing) are ugly.
• Ruby has pretty assignment with operators like += and ||=
![Page 41: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/41.jpg)
# Badarray << 42 unless array.include?(42)array = array + [42] unless array.include?(42)
# Betterarray = array | [42]
# Bestarray |= [42]
![Page 42: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/42.jpg)
WHY?
• Sometimes it just helps to know what set union is.
![Page 43: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/43.jpg)
# Bad
if value != nil && value != false
# Good
if value
![Page 44: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/44.jpg)
WHY?
• Ruby has a sane notion of truthiness
![Page 45: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/45.jpg)
# Bad
if value == 1 || value == 12 || value == 42
# Good
if [1,12,42].include? value
![Page 46: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/46.jpg)
WHY?
• Brevity is not the goal
• Readability is the goal
• But if it is more readable and also shorter, go for it.
![Page 47: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/47.jpg)
# 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
![Page 48: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/48.jpg)
WHY?
• Method definitions are an implied begin block.
![Page 49: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/49.jpg)
# Bad
!!value
# Good
value
![Page 50: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/50.jpg)
WHY?
• Ruby does not not like clarity.
• What you lose in readability you gain in nothing.
![Page 51: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/51.jpg)
# Bad
ActiveRecord::Base
# Good
ActiveRecord::Model
![Page 52: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/52.jpg)
WHY?
• Naming things is important.
• Base? What does that even mean?
• Sorry Rails, you got this one wrong. Better luck next time.
![Page 53: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/53.jpg)
# Bad
class PostsController < ApplicationController
def recent
Post.find :all,
:conditions => ['posts.created_at > ?',
1.week.ago]
end
end
![Page 54: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/54.jpg)
# Good
class PostsController < ApplicationController
def recent
Post.within 1.week
end
end
![Page 55: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/55.jpg)
class Post < ActiveRecord::Base
named_scope :within,
lambda {|seconds| :conditions => ['posts.created_at > ?',
seconds.ago]}
end
![Page 56: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/56.jpg)
WHY?
• Make your code more expressive
• And more intention revealing.
• In other words, say what you mean to say.
![Page 57: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/57.jpg)
url_for(:blog, :posts, @post.id, :comments, :replies => true)
# => http://example.com/blog/posts/19/comments?replies=true
![Page 58: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/58.jpg)
# 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
![Page 59: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/59.jpg)
# Good
def url_for(*args)
root + args.to_params
end
![Page 60: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/60.jpg)
class Array
def to_params
map{|a| a.to_params}.join('/').
gsub('/?', '?')
end
end
![Page 61: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/61.jpg)
# Array
%w(foo bar bazz).to_params # "/foo/bar/bazz"
# Hash
{:foo => :bar}.to_params # "?foo=bar"
![Page 62: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/62.jpg)
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.
![Page 63: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/63.jpg)
RAILS PERFORMANCEInsert your “scaling” and “premature optimization” jokes here.
![Page 64: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/64.jpg)
Yes, I went there.
![Page 65: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/65.jpg)
Slow is only meaningfulin comparison.
![Page 66: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/66.jpg)
Ruby is slow? Compared to what?
![Page 67: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/67.jpg)
Is your database slow?
![Page 68: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/68.jpg)
Are your views slow?
![Page 69: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/69.jpg)
Is your app server slow?
![Page 70: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/70.jpg)
Are you using HTTP via carrier pigeon?
![Page 71: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/71.jpg)
If you don’t know where the slow is, you’re not ready to optimize.
![Page 72: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/72.jpg)
Don’t optimize prematurely, but don’t pessimize either.
![Page 73: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/73.jpg)
Don’t write code you know will never, ever be fast.
![Page 74: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/74.jpg)
# 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
![Page 75: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/75.jpg)
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.
![Page 76: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/76.jpg)
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.
![Page 77: Rails Is From Mars Ruby Is From Venus Presentation 1](https://reader033.vdocuments.net/reader033/viewer/2022061104/5412c3098d7f720c4e8b46b9/html5/thumbnails/77.jpg)
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.