Ruby でアプリケーション例外をつくるときの作り方。
begin
# 通常の処理
rescue SomeErrorException => e
# Exception から継承したクラスや StandardError から継承したクラスを指定で受ける
rescue => e # StandardError
# 継承木に StandardError がいるクラスのみひっかかる
else
Ruby でアプリケーション例外をつくるときの作り方。
begin
# 通常の処理
rescue SomeErrorException => e
# Exception から継承したクラスや StandardError から継承したクラスを指定で受ける
rescue => e # StandardError
# 継承木に StandardError がいるクラスのみひっかかる
else
から自分の環境にあった Ruby の最新版をダウンロード&インストール
制約フレームワークを書かないと連載させてもらえない Web 雑誌があるようなので、昨日こんなの書いてた。
静的解析もできないし、ドキュメンテーションにも活きないので各方面の識者から怒られそうだが {}
で書けるという見た目の点で許してほしい。
あとはつっこまれるとしたら、継承したときに事前条件、事後条件が強化される/緩和されるのあたり。全部やると大変なんすよ。invariant もないですね (method_added でイベントフックしてすべてのメソッドコールへ prepend かぶせるとかすればできるかな)。
class BeerLover
extend Contracts
attr_accessor :age
module Ruby | |
module Spock | |
def spec(description, definition = nil, &proc_definition) | |
raise ArgumentError if [ definition, proc_definition ].all? {|d| d.nil? } | |
if definition.nil? | |
spec_runner(description, proc_definition) | |
else | |
spec_runner(description, definition) | |
end | |
end |
class Foo | |
extend Ruby::Spock | |
spec 'maximum of two numbers', ->(*) { | |
expect ->(a, b, c) { [a, b].max == c } | |
where [ | |
# a | b | c | |
[ 1 , 3 , 3 ], | |
[ 7 , 4 , 4 ], | |
[ 0 , 0 , 0 ], |
def e &blk | |
s = Struct.new(:a,:b,:c).new(1,2,3) | |
s.instance_eval(&blk) | |
end | |
def foo | |
e { a + b + c } | |
end | |
puts foo #=> 6 |
## | |
# 知りたかったこと | |
def foo(a: 1, b: 2, c: 3, d: 4) | |
p a, b, c, d | |
end | |
# 1. bar の引数定義を a~d のキーワード引数として foo へすべて委譲したい | |
# 2. bar の引数定義を a~d のキーワード引数とさらに e: 5 を追加したい | |
def bar( ??? ) |
class Injectee | |
def hello | |
puts :hello | |
end | |
end | |
module DI | |
class Injector | |
def initialize(module_object) | |
@module = module_object |
たとえば、subject でこのように書けて、
subject { model.errors }
it { is_expected.to be > 0 }
これを DRY にしたくて