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
620
3
Share
Ruby Idioms
Florian Plank
February 12, 2014
More Decks by Florian Plank
See All by Florian Plank
Ready, set, immersion!
polarblau
0
220
Prototyping all the things
polarblau
2
190
CoffeeScript vs. ECMAScript 6
polarblau
5
3.7k
Design for a complex Reality — Siili Breakfast Edition
polarblau
0
170
Enabling Design for a Complex Reality
polarblau
2
150
A primer on Content Security Policy
polarblau
1
460
Rails and the future of the open web
polarblau
3
150
Brief Ruby/Ruby on Rails intro
polarblau
3
210
How to ask questions and find the right answers
polarblau
2
390
Other Decks in Programming
See All in Programming
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
4
1.1k
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
180
The NotImplementedError Problem in Ruby
koic
1
510
ビジネスモデルから紐解く、AI+型駆動開発
hirokiomote
2
5.2k
GitHub Copilot CLIのいいところ
htkym
2
1.3k
並列実装の現場、2ヶ月間実務でAIを使い倒したAIもPCも私も限界が近い
ming_ayami
0
100
肥大化するレガシーコードに立ち向かうためのインターフェース分離と依存の逆転 / JJUG CCC 2026 Spring
hirokunimaeta
0
480
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
130
生成AI時代にこそ効くGo | Why Go Works in the Age of Generative AI
mom0tomo
8
3.1k
TAKTでAI駆動開発の品質を設計する
j5ik2o
6
800
Signal Forms: Beyond the Basics @ngBaguette 2026 in Paris
manfredsteyer
PRO
0
220
AI駆動開発で崩れていくコードベースを立て直す
kyoko_nr_nr
1
430
Featured
See All Featured
Building Adaptive Systems
keathley
44
3k
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
320
Making the Leap to Tech Lead
cromwellryan
135
9.9k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
360
Believing is Seeing
oripsolob
1
140
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
130
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
410
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Making Projects Easy
brettharned
120
6.7k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
160
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
930
The Spectacular Lies of Maps
axbom
PRO
1
790
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