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
540
Ruby Idioms
Florian Plank
February 12, 2014
Tweet
Share
More Decks by Florian Plank
See All by Florian Plank
Ready, set, immersion!
polarblau
0
160
Prototyping all the things
polarblau
2
150
CoffeeScript vs. ECMAScript 6
polarblau
5
3.3k
Design for a complex Reality — Siili Breakfast Edition
polarblau
0
110
Enabling Design for a Complex Reality
polarblau
2
110
A primer on Content Security Policy
polarblau
1
340
Rails and the future of the open web
polarblau
3
110
Brief Ruby/Ruby on Rails intro
polarblau
3
150
How to ask questions and find the right answers
polarblau
2
320
Other Decks in Programming
See All in Programming
のびしろを広げる巻き込まれ力:偶然を活かすキャリアの作り方/oso2024
takahashiikki
1
410
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
310
役立つログに取り組もう
irof
27
8.7k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
570
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4.1k
リリース8年目のサービスの1800個のERBファイルをViewComponentに移行した方法とその結果
katty0324
5
3.7k
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
1.7k
約9000個の自動テストの 時間を50分->10分に短縮 Flakyテストを1%以下に抑えた話
hatsu38
24
11k
offers_20241022_imakiire.pdf
imakurusu
2
370
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
960
WEBエンジニア向けAI活用入門
sutetotanuki
0
300
CSC509 Lecture 08
javiergs
PRO
0
110
Featured
See All Featured
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
355
29k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Adopting Sorbet at Scale
ufuk
73
9k
GitHub's CSS Performance
jonrohan
1030
460k
Six Lessons from altMBA
skipperchong
26
3.5k
Designing on Purpose - Digital PM Summit 2013
jponch
115
6.9k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
290
What's new in Ruby 2.0
geeforr
342
31k
[RailsConf 2023] Rails as a piece of cake
palkan
51
4.9k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
43
6.6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.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