View
225
Download
0
Category
Preview:
Citation preview
8/14/2019 Rails Is from Mars, Ruby Is from Venus
1/77
8/14/2019 Rails Is from Mars, Ruby Is from Venus
2/77
WHO AM I?
My name is Rein Henrichs.
Thats 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.
https://twitter.com/home?status=@ReinHhttp://reinh.com/https://twitter.com/home?status=@ReinHhttp://reinh.com/http://reinh.com/https://twitter.com/home?status=@ReinH8/14/2019 Rails Is from Mars, Ruby Is from Venus
3/77
YOU KNOW RAILS
insert logohere:(
8/14/2019 Rails Is from Mars, Ruby Is from Venus
4/77
BUT DO YOU KNOW RUBY?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
5/77
How many people here use Rails?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
6/77
How many of you think you knowRuby as well as you know Rails?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
7/77
How many of you have
contributed to an open-sourceRuby project?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
8/77
How many of you havewritten your own gem?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
9/77
How many of you would be
comfortable writing an HTTPclient library in Ruby?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
10/77
How many of you could writeyour own web framework?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
11/77
WHY SHOULD I LEARN RUBY?
Its easy.
Its fun.
It will make you better at Rails.
It will make you a better person.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
12/77
HOW DO I LEARN RUBY?Some resources
8/14/2019 Rails Is from Mars, Ruby Is from Venus
13/77
Free stuff
8/14/2019 Rails Is from Mars, Ruby Is from Venus
14/77
WHYS POIGNANT GUIDEIt has car toon foxes. Foxen? It has cartoon foxen.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
15/77
8/14/2019 Rails Is from Mars, Ruby Is from Venus
16/77
MR. NEIGHBORLYSHUMBLE LITTLE
RUBY BOOKPragmatically chunky bacon
8/14/2019 Rails Is from Mars, Ruby Is from Venus
17/77
Not so free stuff(but still really good)
8/14/2019 Rails Is from Mars, Ruby Is from Venus
18/77
RUBY IN A
NUTSHELLCome on. Matz wrote it.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
19/77
RUBY IN
PRACTICELook at that funny looking guyon the cover. At least its not anasty monkey. Sorry, OReilly
8/14/2019 Rails Is from Mars, Ruby Is from Venus
20/77
THE RUBY
WAY
8/14/2019 Rails Is from Mars, Ruby Is from Venus
21/77
RUBY FOR
RAILS
8/14/2019 Rails Is from Mars, Ruby Is from Venus
22/77
THE WELL-GROUNDED
RUBYIST
8/14/2019 Rails Is from Mars, Ruby Is from Venus
23/77
RAILS HAS OPINIONSAnd Ruby Likes To Talk
8/14/2019 Rails Is from Mars, Ruby Is from Venus
24/77
Rails is opinionated software
8/14/2019 Rails Is from Mars, Ruby Is from Venus
25/77
Ruby is a communicative language
8/14/2019 Rails Is from Mars, Ruby Is from Venus
26/77
Sometimes, Ruby just wantssomeone to listen to it.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
27/77
If youre programming along, doing nicely, andall of a sudden your program gets balky, makes
things hard for you, its talking. Its telling youthere is something important missing.
Kent Beck, Smalltalk Best Practice Patterns
8/14/2019 Rails Is from Mars, Ruby Is from Venus
28/77
It is your responsibility to
listen to your code and beconsiderate of its needs.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
29/77
Write Ruby code that
communicates well but berespectful of Rails opinions
8/14/2019 Rails Is from Mars, Ruby Is from Venus
30/77
Other people who use your code
(including six-months-later you)will thank you.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
31/77
Ruby makes it easy to writesimply, clearly and expressively
8/14/2019 Rails Is from Mars, Ruby Is from Venus
32/77
Rails has powerful idiomsand conventions
8/14/2019 Rails Is from Mars, Ruby Is from Venus
33/77
Combining the two makes for ahappy, fulfilling relationship
8/14/2019 Rails Is from Mars, Ruby Is from Venus
34/77
RUBY LOVES YOUBut Sometimes You Drive Her Crazy
8/14/2019 Rails Is from Mars, Ruby Is from Venus
35/77
These are some of the things youdo that drive Ruby crazy.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
36/77
Youre welcome.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
37/77
# Bad
i = 0; while i < array.size do
puts array[i]i += 1
end
# Better
for item in array
puts item
end
# Bestarray.each do |item|
puts item
end
8/14/2019 Rails Is from Mars, Ruby Is from Venus
38/77
WHY?
Ruby has powerful iterators.
You dont need to write your own.
for ... in ... just calls #each internally.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
39/77
# Bad
value = value ? value : "default"
# Bettervalue = value || "default"
# Best
value ||= "default"
8/14/2019 Rails Is from Mars, Ruby Is from Venus
40/77
WHY?
Ternaries (the ? : thing) are ugly.
Ruby has pretty assignment with operators like += and ||=
8/14/2019 Rails Is from Mars, Ruby Is from Venus
41/77
# Bad
array
8/14/2019 Rails Is from Mars, Ruby Is from Venus
42/77
WHY?
Sometimes it just helps to know what set union is.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
43/77
# Bad
if value != nil && value != false
# Good
if value
8/14/2019 Rails Is from Mars, Ruby Is from Venus
44/77
WHY?
Ruby has a sane notion of truthiness
8/14/2019 Rails Is from Mars, Ruby Is from Venus
45/77
# Bad
if value == 1 || value == 12 || value == 42
# Good
if [1,12,42].include? value
8/14/2019 Rails Is from Mars, Ruby Is from Venus
46/77
WHY?
Brevity is not the goal
Readability is the goal
But if it is more readable and also shorter, go for it.
# Bad
8/14/2019 Rails Is from Mars, Ruby Is from Venus
47/77
# Bad
def request
begin
perform_requestrescue RequestError => e
log_error e
end
end
# Good
def request
perform_request
rescue RequestError => e
log_error e
end
8/14/2019 Rails Is from Mars, Ruby Is from Venus
48/77
WHY?
Method definitions are an implied begin block.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
49/77
# Bad!!value
# Good
value
8/14/2019 Rails Is from Mars, Ruby Is from Venus
50/77
WHY?
Ruby does not not like clarity.
What you lose in readability you gain in nothing.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
51/77
# Bad
ActiveRecord::Base
# Good
ActiveRecord::Model
8/14/2019 Rails Is from Mars, Ruby Is from Venus
52/77
WHY?
Naming things is important.
Base? What does that even mean?
Sorry Rails, you got this one wrong. Better luck next time.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
53/77
# Bad
class PostsController < ApplicationController
def recent
Post.find :all, :conditions => ['posts.created_at > ?',
1.week.ago]
end
end
8/14/2019 Rails Is from Mars, Ruby Is from Venus
54/77
# Good
class PostsController < ApplicationController
def recentPost.within 1.week
end
end
8/14/2019 Rails Is from Mars, Ruby Is from Venus
55/77
class Post < ActiveRecord::Base
named_scope :within,
lambda {|seconds| :conditions => ['posts.created_at > ?',seconds.ago]}
end
8/14/2019 Rails Is from Mars, Ruby Is from Venus
56/77
WHY?
Make your code more expressive
And more intention revealing.
In other words, say what you mean to say.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
57/77
url_for(:blog, :posts, @post.id, :comments, :replies => true)# => http://example.com/blog/posts/19/comments?replies=true
http://example.com/blog/posts/19/comments?replies=truehttp://example.com/blog/posts/19/comments?replies=true8/14/2019 Rails Is from Mars, Ruby Is from Venus
58/77
# 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
8/14/2019 Rails Is from Mars, Ruby Is from Venus
59/77
# Good
def url_for(*args)root + args.to_params
end
8/14/2019 Rails Is from Mars, Ruby Is from Venus
60/77
8/14/2019 Rails Is from Mars, Ruby Is from Venus
61/77
# Array
%w(foo bar bazz).to_params # "/foo/bar/bazz"
# Hash
{:foo => :bar}.to_params # "?foo=bar"
8/14/2019 Rails Is from Mars, Ruby Is from Venus
62/77
WHY?
Ruby uses coercion in many places
1.to_s
(1..10).to_a
Writing your own coercion method can help you use Rubysducktyping.
Separation of concerns.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
63/77
RAILS PERFORMANCEInsert your scaling and premature optimization jokes here.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
64/77
Yes, I went there.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
65/77
Slow is only meaningfulin comparison.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
66/77
Ruby is slow? Compared to what?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
67/77
Is your database slow?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
68/77
Are your views slow?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
69/77
Is your app server slow?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
70/77
Are you using HTTPvia carrier pigeon?
8/14/2019 Rails Is from Mars, Ruby Is from Venus
71/77
If you dont know where the slowis, youre not ready to optimize.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
72/77
Dont optimize prematurely, butdont pessimize either.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
73/77
Dont write code you know willnever, ever be fast.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
74/77
# 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 votesdefault_scope :order => :votes_count
end
8/14/2019 Rails Is from Mars, Ruby Is from Venus
75/77
8/14/2019 Rails Is from Mars, Ruby Is from Venus
76/77
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, dont be premature. No one likes that.
8/14/2019 Rails Is from Mars, Ruby Is from Venus
77/77
WHO AM I?
My name is Rein Henrichs.
Thats pronounced like rain.
But spelled differently.
I work at
I blog at reinh.com
I twitter @reinh
I lik dl lit di d l lk th b h
https://twitter.com/home?status=@ReinHhttp://reinh.com/https://twitter.com/home?status=@ReinHhttp://reinh.com/http://reinh.com/https://twitter.com/home?status=@ReinHRecommended