Skip to content

Instantly share code, notes, and snippets.

@kyohei-shimada
kyohei-shimada / file0.txt
Last active February 3, 2017 04:20
rubocopが一切使われていないRailsアプリにrubocopを段階的に導入していく際にやったこと/やりたいこと ref: http://qiita.com/kyohei_shimada/items/e739dec967eb5e61721c
bundle exec rubocop -D -R
# ...
629 files inspected, 8475 offenses detected
require 'active_support'
require 'active_support/core_ext/hash/slice'
# キーを2つ指定したつもりが単なる文字列になってしまっていた...
{ 'key1' => 'hoge', 'key2' => 'fuga', 'key3' => 'piyo' }.slice(*" key1 key2 ")
#=> { }
@kyohei-shimada
kyohei-shimada / condition.md
Last active June 5, 2019 09:47
rubyの条件分岐

Rubyの条件分岐に関するオレオレまとめ

前提

  • 分岐は少なければ少ないほうがよい
  • とはいえ絶対に必要なのでどうすればよいか?
    • ネストを減らす
      • 分岐を仮に愚直にやっていくとn乗オーダーで処理が分かれていき,とてもじゃないが読めない
    • 統一感,対称性を持たせる
    • 適切なメソッド化をする
    • 分岐は処理の流れが変わるという重要な役割をもつため.分岐をするのであれば分岐をするという役割でメソッドを1つ切る(dispatcherが典型)
@kyohei-shimada
kyohei-shimada / argument.md
Last active April 21, 2024 05:40
rubyの引数のお話

引数

通常の引数

def hoge(a, b)
  [a, b]
end

hoge(1)         # ArgumentError: wrong number of arguments (1 for 2)
@kyohei-shimada
kyohei-shimada / xor_image.rb
Created May 20, 2015 16:27
2つの画像のxorをとるテスト
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透過色を無視
@kyohei-shimada
kyohei-shimada / rails_memo.md
Last active August 29, 2015 14:21
RailsのちょっとしたTips

Railsじゃないのばっかりな気がするけど,Rails使ってる途中で思ったことなど

ActiveSupport

Hash#slice, slice!

ハッシュから指定したキーのみを取り出したハッシュを取得

{ a: 1, b: 2, c: 3, d: 4 }.slice(:b, :c) #=> {:b=>2, :c=>3}
@kyohei-shimada
kyohei-shimada / ruby_memo.md
Last active February 27, 2020 06:27
RubyのちょっとしたTips

RubyのちょとしたTips

ちょっと便利なことだったり,今さら知った恥ずかしいことなど

ローカル変数のスコープ

小文字または`_'で始まる識別子はローカル変数また はメソッド呼び出しです。ローカル変数スコープ(クラス、モジュー ル、メソッド定義の本体)における小文字で始まる識別子への最初 の代入はそのスコープに属するローカル変数の宣言になります。宣 言されていない識別子の参照は引数の無いメソッド呼び出しとみな されます。 ローカル変数のスコープは、((宣言した位置から))その変数が宣 言されたブロック、メソッド定義、またはクラス/モジュール定義 の終りまでです。寿命もそのブロックの終りまで(トップレベルの ローカル変数はプログラムの終了まで)ですが、例外としてブロッ クが手続きオブジェクト化された場合は、そのオブジェクトが消滅 するまで存在します。同じスコープを参照する手続きオブジェクト 間ではローカル変数は共有されます。

@kyohei-shimada
kyohei-shimada / capybara_memo.md
Last active February 12, 2023 23:49
Capybaraを使う上で気をつけること

Capybaraを使う上で気をつけること

アンチパターンとか,ハマりどころとか

expect(page).to have_content 'hogheoge'

expect(page).to have_content 'hogehoge'
@kyohei-shimada
kyohei-shimada / application_helper.rb
Last active December 9, 2015 23:29
#{Rails.root}/app/helper/application_helper.rb
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>