Skip to content

Instantly share code, notes, and snippets.

@sunaot
sunaot / segfault_spec.rb
Created November 13, 2013 11:24
rspec で Ruby が segmentation fault 吐いた (Ruby: 2.0.0p195/2.0.0-p247, RSpec: 2.14.7)
require 'bundler/setup'
module ExtendModule
def foo
'foo'
end
def bar
'bar'
end
@sunaot
sunaot / product.rb
Created January 16, 2014 13:09
全網羅のテストをしたいときこんなんすると組合せをつくってくれて便利。
test_cases = [
%w[ docomo softbank au emobile willcom ],
%w[ development production testing staging ],
%w[ jp uk de ],
]
test_cases.first.product(*test_cases[1..-1]) do |carrier, env, region|
p carrier: carrier,
env: env,
region: region
end
@sunaot
sunaot / eigenclass.md
Last active July 4, 2019 02:32
class << self によるクラスメソッドの定義のイディオムの説明とその背景にある特異クラスのかんたんな解説

Ruby のコードを読んでいると

class Hoge
  class << self
    def hello
      puts 'hello'
    end
  end
end
@sunaot
sunaot / exception_in_ruby.md
Created March 10, 2014 11:31
Ruby で Exception

Ruby でアプリケーション例外をつくるときの作り方。

begin
  # 通常の処理
rescue SomeErrorException => e
  # Exception から継承したクラスや StandardError から継承したクラスを指定で受ける
rescue => e # StandardError
  # 継承木に StandardError がいるクラスのみひっかかる
else
@sunaot
sunaot / capybara_win.md
Created April 10, 2014 10:30
capybara on windows

制約フレームワークを書かないと連載させてもらえない Web 雑誌があるようなので、昨日こんなの書いてた。 静的解析もできないし、ドキュメンテーションにも活きないので各方面の識者から怒られそうだが {} で書けるという見た目の点で許してほしい。

あとはつっこまれるとしたら、継承したときに事前条件、事後条件が強化される/緩和されるのあたり。全部やると大変なんすよ。invariant もないですね (method_added でイベントフックしてすべてのメソッドコールへ prepend かぶせるとかすればできるかな)。

class BeerLover
  extend Contracts
  attr_accessor :age
@sunaot
sunaot / hello_ruby.md
Last active April 4, 2017 11:04
さらっと読んで Ruby がわかった気になれるプログラミング言語 Ruby のかんたんな紹介
@sunaot
sunaot / spock-like.rb
Last active August 29, 2015 14:07
spock ぽい何かの Ruby での習作
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
@sunaot
sunaot / spock-like-spec.rb
Created October 14, 2014 16:05
Spock のようななにかを Ruby で。の仮実装。これは spec 側。実装は https://gist.github.com/sunaot/9efce49897e3dbf98fca にあります。
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 ],