Skip to content

Instantly share code, notes, and snippets.

@chezou
Created January 30, 2014 15:03
Show Gist options
  • Select an option

  • Save chezou/8710334 to your computer and use it in GitHub Desktop.

Select an option

Save chezou/8710334 to your computer and use it in GitHub Desktop.
classとメソッドの設計は適当ですが、こんなイメージです。
count = 0
if target_user_ids.empty?
count += Foo.all.count
else
target_user_ids.each do |user_ids|
count += Foo.where(user_id: user_ids).count
end
end
class Foo
def func(arg1, arg2)
return arg1 if arg2.empty?
arg2.each_slice(2).map do |e2|
arg1 & e2
end.flatten
end
end
require 'spec_helper'
describe 'Foo' do
let(:foo) { Foo.new }
context 'with empty array' do
let(:ary1) { [4,3,2,1] }
let(:ary2) { [] }
it 'returns whole ary1' do
expect(foo.func(ary1, ary2)).to eq(ary1)
end
end
context 'without empty array' do
let(:ary1) { [6,4,3,1] }
let(:ary2) { [1,2,4] }
it 'returns ary1 slected ary2' do
expect(foo.func(ary1, ary2)).to eq([1,4])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment