Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby Idioms
Search
Florian Plank
February 12, 2014
Programming
640
3
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby Idioms
Florian Plank
February 12, 2014
Transcript
55 slides
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
More Decks by Florian Plank
See All by Florian Plank
Ready, set, immersion!
polarblau
0
230
Prototyping all the things
polarblau
2
210
CoffeeScript vs. ECMAScript 6
polarblau
5
3.7k
Design for a complex Reality — Siili Breakfast Edition
polarblau
0
190
Enabling Design for a Complex Reality
polarblau
2
180
A primer on Content Security Policy
polarblau
1
510
Rails and the future of the open web
polarblau
3
160
Brief Ruby/Ruby on Rails intro
polarblau
3
230
How to ask questions and find the right answers
polarblau
2
410
Other Decks in Programming
See All in Programming
Secure Your Coding Agent Like It’s Malware (v1.5) 🇬🇧 @Code.talks 2026
richargh
0
190
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
220
Vue.jsで作る空間解析アプリとCapacitorプラグインのData Bridge
hal_spidernight
1
180
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
340
Snowflake Openflow Connector利用の勘所 / 20261024developersio2026tokyo-snowflake-openflow
kasacchiful
PRO
0
360
Distance field of Julia set
kinakomoti321
0
330
LLM 101: Exploring LLMs, Starting with Tokens.
dalinaum
0
110
우리의 Nitro Modules는 당신의 Turbo Modules보다 아름답다
l2hyunwoo
0
270
地方こそサーバーレス — 10年続けた、顔が見えるチームで確かな基盤を / Serverless Matters Most in Regional Areas — 10 Years of Building Reliable Foundations with Close-Knit Teams
seike460
PRO
2
490
Enforcing Your Architecture with Unit Tests in the AI Era (Swift Connection X)
steliosf
PRO
1
190
Spec-driven Development with AI
simas
PRO
0
2.3k
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
350
Featured
See All Featured
Joys of Absence: A Defence of Solitary Play
codingconduct
1
580
Test your architecture with Archunit
thirion
2
2.5k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.8k
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
680
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.3k
Navigating Team Friction
lara
192
16k
Ten Tips & Tricks for a 🌱 transition
stuffmc
1
290
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
1
670
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
ラッコキーワード サービス紹介資料
rakko
5
5.9M
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3.1k