- 分岐は少なければ少ないほうがよい
- とはいえ絶対に必要なのでどうすればよいか?
- ネストを減らす
- 分岐を仮に愚直にやっていくとn乗オーダーで処理が分かれていき,とてもじゃないが読めない
- 統一感,対称性を持たせる
- 適切なメソッド化をする
- 分岐は処理の流れが変わるという重要な役割をもつため.分岐をするのであれば分岐をするという役割でメソッドを1つ切る(dispatcherが典型)
- ネストを減らす
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle exec rubocop -D -R | |
# ... | |
629 files inspected, 8475 offenses detected |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support' | |
require 'active_support/core_ext/hash/slice' | |
# キーを2つ指定したつもりが単なる文字列になってしまっていた... | |
{ 'key1' => 'hoge', 'key2' => 'fuga', 'key3' => 'piyo' }.slice(*" key1 key2 ") | |
#=> { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'chunky_png' | |
# 画像サイズと色の種類は同じと仮定 | |
image1 = ChunkyPNG::Image.from_file('sample1.png') | |
image2 = ChunkyPNG::Image.from_file('sample2.png') | |
xor_image = ChunkyPNG::Image.new(image1.width, image1.height, ChunkyPNG::Color::TRANSPARENT) | |
image1.height.times do |h| | |
image2.width.times do |w| | |
# 0xff透過色を無視 |
ちょっと便利なことだったり,今さら知った恥ずかしいことなど
小文字または`_'で始まる識別子はローカル変数また はメソッド呼び出しです。ローカル変数スコープ(クラス、モジュー ル、メソッド定義の本体)における小文字で始まる識別子への最初 の代入はそのスコープに属するローカル変数の宣言になります。宣 言されていない識別子の参照は引数の無いメソッド呼び出しとみな されます。 ローカル変数のスコープは、((宣言した位置から))その変数が宣 言されたブロック、メソッド定義、またはクラス/モジュール定義 の終りまでです。寿命もそのブロックの終りまで(トップレベルの ローカル変数はプログラムの終了まで)ですが、例外としてブロッ クが手続きオブジェクト化された場合は、そのオブジェクトが消滅 するまで存在します。同じスコープを参照する手続きオブジェクト 間ではローカル変数は共有されます。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ApplicationHelper | |
# [example] | |
# simple_table(["A", "B", "C"], [ ["a1", "b1", "c1"], ["a2", "b2", "c2"] ], {:table => { :id => "hoge", :class => "huga"}, :tr => { :class => "piyo"}}) | |
# => | |
# <table id="hoge" class="huga"> | |
# <thead> | |
# <tr class="piyo"> | |
# <th>A</th> | |
# <th>B</th> | |
# <th>C</th> |