Skip to content

Instantly share code, notes, and snippets.

@hzm-s
hzm-s / validator_spec.rb
Created September 3, 2012 02:50
DRY'uped validator spec
require 'spec_helper'
shared_examples "valid" do
it { expect { subject }.not_to raise_error(Avalon::Error) }
end
shared_examples "invalid" do
it { expect { subject }.to raise_error(Avalon::Error) }
end
@hzm-s
hzm-s / gist:8313204
Last active January 2, 2016 13:58
Structなクラスをハッシュでnew
class Lens < Struct.new(:name, :mm, :f, :close_up, :generation)
def initialize(attrs)
super(*self.class.members.inject([]) {|r, x| r << attrs[x] })
end
end
Lens.new(
f: 2,
generation: '3rd',
@hzm-s
hzm-s / create_spec.rb
Last active January 3, 2016 04:49
異常系ValidationのE2Eテスト(RSpec,Rails)
context '入力が正しくない' do
[
[:name, "未入力", nil, "can't be blank"],
[:name, "101文字以上", 'a' * 101, "is too long"],
[:display, "101文字以上", 'a' * 101, "is too long"]
]
.each do |attr, state, value, msg|
context "#{attr.to_s.humanize}が#{state}" do
let(:input) { valid_input.merge(attr => value) }
@hzm-s
hzm-s / show_list_spec.rb
Last active January 3, 2016 05:09
簡易的な一覧画面のレコード表示のテスト(RSpec,Rails)
before do
@resources = FactoryGirl.create_list(:resource, 10)
end
it 'それぞれのResourceが表示されていること' do
visit '/resources'
@resources.each do |r|
expect(page).to have_content(/#{r.id}\s+#{r.attr1}\s+#{r.attr2}\s+#{r.attr3}/)
end
end
@hzm-s
hzm-s / group_by.pl
Created February 13, 2014 09:18
PerlでRubyのEnumerable#group_by
sub group_by(&@) {
my $block = shift;
my %result;
for (@_) {
my $key = $block->($_);
$result{$key} = [] unless exists $result{$key};
push @{ $result{$key} }, $_;
}
@hzm-s
hzm-s / gist:9908372
Created April 1, 2014 05:46
MSS Interaction
#logs
@timeline = Timeline.find(current_user)
@timeline.each do |activity|
p activity.date
p activity.balance
p activity.outgo
p activity.income
end
uname -a
@hzm-s
hzm-s / mixin.go
Last active April 13, 2022 10:50
mixin with golang
package main
import "fmt"
// 料理を作るI/F
type Cooker interface {
Cook() string
}
// 塗るI/F
class KaraokeMachine
def initialize(melody_string)
@melody = Melody.parse(melody_string)
end
def transpose(amount)
@melody.transpose(amount).present
end
end
@hzm-s
hzm-s / i_f.go
Created August 12, 2014 12:59
Goのインタフェースとその実装、およびstructへの埋め込み ref: http://qiita.com/haazime/items/2cc00097215ee8828224
package main
import "fmt"
// 料理を作るI/F
type Cooker interface {
Cook() string
}
// 塗るI/F