Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby Idioms
Search
Florian Plank
February 12, 2014
Programming
3
570
Ruby Idioms
Florian Plank
February 12, 2014
Tweet
Share
More Decks by Florian Plank
See All by Florian Plank
Ready, set, immersion!
polarblau
0
170
Prototyping all the things
polarblau
2
160
CoffeeScript vs. ECMAScript 6
polarblau
5
3.5k
Design for a complex Reality — Siili Breakfast Edition
polarblau
0
130
Enabling Design for a Complex Reality
polarblau
2
130
A primer on Content Security Policy
polarblau
1
390
Rails and the future of the open web
polarblau
3
120
Brief Ruby/Ruby on Rails intro
polarblau
3
170
How to ask questions and find the right answers
polarblau
2
340
Other Decks in Programming
See All in Programming
マテリアルって何者?RealityKitで扱うマテリアル入門
nao_randd
0
100
Modern Angular with Signals and Signal Store:New Rules for Your Architecture @jax2025 in Mainz, Germany
manfredsteyer
PRO
0
110
データと事例で振り返るDevin導入の"リアル" / The Realities of Devin Reflected in Data and Case Studies
rkaga
3
2.8k
The New Developer Workflow: How AI Transforms Ideas into Code
danielsogl
0
140
VibeCoding時代のエンジニアリング
daisuketakeda
0
270
クラシルリワードにおける iOSアプリ開発の取り組み
funzin
1
230
Cursor/Devin全社導入の理想と現実
saitoryc
29
22k
Browser and UI #2 HTML/ARIA
ken7253
2
190
インプロセスQAにおいて大事にしていること / In-process QA Meetup
medley
0
190
開発者フレンドリーで顧客も満足?Platformの秘密
algoartis
0
250
AI時代のリアーキテクチャ戦略 / Re-architecture Strategy in the AI Era
dachi023
0
160
データベースの技術選定を突き詰める ~複数事例から考える最適なデータベースの選び方~
nnaka2992
3
3k
Featured
See All Featured
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
10
810
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
Navigating Team Friction
lara
185
15k
Measuring & Analyzing Core Web Vitals
bluesmoon
7
430
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
29
1.7k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
The Art of Programming - Codeland 2020
erikaheidi
54
13k
4 Signs Your Business is Dying
shpigford
183
22k
Agile that works and the tools we love
rasmusluckow
329
21k
A Modern Web Designer's Workflow
chriscoyier
693
190k
Transcript
RUBY & RUBY ON RAILS IDIOMS
A word on style
- Soft tabs, two space indent - Lines shorter than
80 characters - No spaces after [( and before )] - Methods: snake_case - Classes/Modules: CamelCase - Constants: SCREAMING_SNAKE_CASE - Space after commas: foo(a, b)
“Idioms”
if answer == 42 # ... elsif foo == 'foo'
# ... else # ... end
if answer == 42 then # ... elsif foo ==
'foo' then # ... else # ... end
if answer == 42 then # ... elsif foo ==
'foo' then # ... else # ... end
if !correct # ... end
unless correct # ... end
unless correct # ... else # ... end
unless correct single_line_of_something end
single_line_of_something unless correct single_line_of_something if correct
message = if correct "You are right!" else "Sorry, that's
wrong." end
if !flaky_value.nil? # ... end
if flaky_value # ... end
if env == :production or env == :development # ...
end
if [:production, :development].include?(env) # ... end
String(123)
123.to_s
123.is_a? String
123.respond_to? :to_s
def destroy_universe # ... end destroy_universe def destroy_planet(planet) # ...
end destroy_planet(:mars)
destroy_universe()
def foo(arg1, arg2) # ... end foo 1, 2
def foo(options, &block) # ... end foo {}
def green? # ... end def destroy! # ... end
def foo(name, options) # ... end foo('bar', {:something => 'else',
:answer => 42}) foo('bar', :something => 'else', :answer => 42)
def foo # ... body return results end
def foo(arg) return unless arg == 42 # ... body
end
a, b = b, a
a, b = [1, 2, 3]
%w(helsinki oulu tampere) # => ["helsinki", "oulu", "tampere"]
%i(helsinki oulu tampere) # => [:helsinki, :oulu, :tampere]
%i|helsinki oulu tampere| %i-helsinki oulu tampere-
the_number ||= complex_calculation
list = [] dictionary = {}
class Store class << self def advanced_search # ... end
end end
Struct.new("Point", :x, :y)
class Point < Struct.new(:x, :y) end origin = Point.new(0,0)
Point = Struct.new(:x, :y) do def to_s "[#{x}, #{y}]" end
end Point.new(0,0).to_s # => "[0, 0]"
class Parent @@class_var = "parent" def self.print_class_var puts @@class_var end
end class Child < Parent @@class_var = "child" end Parent.print_class_var # => "child"
upcase_chars = [] %w(a b c).each do |char| upcase_chars <<
char.upcase end Not so great
%w(a b c).map do |char| char.upcase end Better
%w(a b c).map { |char| char.upcase } Even better
%w(a b c).map(&:upcase) Great
Brand.first.stores.map(&:id) Brand.first.store_ids @active_brands.ids
def make_it_bigga(string) string.upcase end %w(a b c).map(&method(:make_it_bigga))
[1, 2, nil, 4].compact.reject(&:odd?).inject(&:+) # => 6
{:foo => "bar", :baz => 42}.each do |key, value| puts
"#{key}: #{value}" end # foo: bar # baz: 42 # => {:foo=>"bar", :baz=>42}
[[:foo, "bar"], [:baz, 42]].each do |(key, value)| puts "#{key}: #{value}"
end # foo: bar # baz: 42 # => {:foo=>"bar", :baz=>42}
[[:foo, "bar"], [:baz, 42]].each do |(_, value)| puts value end
# bar # 42 # => [[:foo, "bar"], [:baz, 42]]
name == "" name.length == 0 name.empty?
count == 0 count.zero?
dangerous rescue nil
FOLLOW YOUR