Rails Is From Mars Ruby Is From Venus Presentation 1
1. RAILS IS FROM MARS
RUBY IS FROM VENUS
Relationship Advice For Rails Developers
2. 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.
27. 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
28. It is your responsibility to
listen to your code and be
considerate of its needs.
29. Write Ruby code that
communicates well but be
respectful of Rails’ opinions
30. Other people who use your code
(including six-months-later you)
will thank you.
31. Ruby makes it easy to write
simply, clearly and expressively
45. # Bad
if value == 1 || value == 12 || value == 42
# Good
if [1,12,42].include? value
46. WHY?
• Brevity is not the goal
• Readability is the goal
• But if it is more readable and also shorter, go for it.
47. # 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
48. WHY?
• Method definitions are an implied begin block.
58. # 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
62. 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.
74. # 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
75. 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.
76. 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.
77. 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.