From 22133ddebf6f3c9390453f659b6b2522ef001fa6 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Mon, 23 Jun 2014 11:06:53 -0400 Subject: [PATCH] Add guideline on symbol to_proc * Using &:method_name is brief but clear * Using it consistently aids readability * Preferring simple method calls encourages simple methods --- style/README.md | 2 ++ style/samples/ruby.rb | 8 ++++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/style/README.md b/style/README.md index 9f4ed75a..5507b501 100644 --- a/style/README.md +++ b/style/README.md @@ -156,6 +156,8 @@ Ruby * Prefer double quotes for strings. * Prefer `&&` and `||` over `and` and `or`. * Prefer `!` over `not`. +* Prefer `&:method_name` to `{ |item| item.method_name }` for simple method + calls. * Use `_` for unused block parameters. * Use `%{}` for single-line strings needing interpolation and double-quotes. * Use `{...}` for single-line blocks. Use `do..end` for multi-line blocks. diff --git a/style/samples/ruby.rb b/style/samples/ruby.rb index 52581598..682fda67 100644 --- a/style/samples/ruby.rb +++ b/style/samples/ruby.rb @@ -23,8 +23,12 @@ def method_with_multiline_block some_method_after_block(should_follow_after_newline) end - def method_with_single_line_block - items.map { |item| item.some_attribute } + def method_with_single_method_block + items.map(&:some_attribute) + end + + def method_with_oneline_combined_methods_block + items.map { |item| "#{item.one} #{item.two}" } end def method_that_returns_an_array