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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Florian Plank
February 12, 2014
Programming
610
3
Share
Ruby Idioms
Florian Plank
February 12, 2014
More Decks by Florian Plank
See All by Florian Plank
Ready, set, immersion!
polarblau
0
200
Prototyping all the things
polarblau
2
190
CoffeeScript vs. ECMAScript 6
polarblau
5
3.6k
Design for a complex Reality — Siili Breakfast Edition
polarblau
0
170
Enabling Design for a Complex Reality
polarblau
2
140
A primer on Content Security Policy
polarblau
1
450
Rails and the future of the open web
polarblau
3
140
Brief Ruby/Ruby on Rails intro
polarblau
3
210
How to ask questions and find the right answers
polarblau
2
370
Other Decks in Programming
See All in Programming
PHPで TLSのプロトコルを実装してみる
higaki_program
0
730
GC言語のWasm化とComponent Modelサポートの実践と課題 - Scalaの場合
tanishiking
0
140
PHP 7.4でもOpenTelemetryゼロコード計装がしたい! / PHPerKaigi 2026
arthur1
1
460
Linux Kernelの1文字のミスで 権限昇格ができた話
rqda
0
2.2k
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
260
「接続」—パフォーマンスチューニングの最後の一手 〜点と点を結ぶ、その一瞬のために〜
kentaroutakeda
5
2.4k
AI Assistants for YourAngular Solutions @Angular Graz, March 2026
manfredsteyer
PRO
0
140
Reactive ❤️ Loom: A Forbidden Love Story
franz1981
2
210
Smarter Angular mit Transformers.js & Prompt API
christianliebel
PRO
1
110
今年もTECHSCOREブログを書き続けます!
hiraoku101
0
220
Migration to Signals, Signal Forms, Resource API, and NgRx Signal Store @Angular Days 03/2026 Munich
manfredsteyer
PRO
0
210
野球解説AI Agentを開発してみた - 2026/02/27 LayerX社内LT会資料
shinyorke
PRO
0
390
Featured
See All Featured
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Why Our Code Smells
bkeepers
PRO
340
58k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
1.8k
A better future with KSS
kneath
240
18k
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
390
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Winning Ecommerce Organic Search in an AI Era - #searchnstuff2025
aleyda
1
1.9k
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
61
43k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.1k
Data-driven link building: lessons from a $708K investment (BrightonSEO talk)
szymonslowik
1
990
Paper Plane (Part 1)
katiecoart
PRO
0
6.3k
Abbi's Birthday
coloredviolet
2
6.2k
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