Skip to content

Instantly share code, notes, and snippets.

@ukstudio
ukstudio / battle.rb
Created April 11, 2012 08:40
battle.rb
class User
has_one :character
end
class Character
belongs_to :user
has_many :battles
has_many :battle_records
end
@ukstudio
ukstudio / rspec_http_mathers.rb
Created March 5, 2012 03:51
rspec-http-matchers
Rack::Utils::SYMBOL_TO_STATUS_CODE.each do |symbol,code|
matcher_name = symbol.to_s =~ /^not_/ ? symbol : "be_#{symbol}"
RSpec::Matchers.define matcher_name do
match do |response|
response.code.to_i == code
describe Fixnum do
subject { [1,2,3,4] }
specify { subject.all?{|v| v==1} }
it { should_not be_all{|v| v==1 }}
it { should be_any{|v| v==1 } }
it { should be_any(&lambda{|v| v==1 }) }
end
class Foo
@ukstudio
ukstudio / compose.rb
Created February 3, 2012 06:48
compose
# lambda/proc ver
class Proc
def self.compose(f, g)
lambda {|*args| f[g[*args]] }
end
def <<(g)
Proc.compose(self,g)
end
@ukstudio
ukstudio / test.rb
Created February 2, 2012 05:30
alias
class Foo
def self.show
'foo'
end
class << self
def show_with_bar
"#{show_without_bar} bar"
end
alias :show_without_bar :show
@ukstudio
ukstudio / test.rb
Created February 2, 2012 05:28 — forked from mashiro/test.rb
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
class Foo
def self.show
'foo'
end
end
module Bar
user nginx nginx;
worker_processes 1;
error_log /var/log/nginx/error_log info;
events {
worker_connections 1024;
use epoll;
}
require 'net/http'
uri = URI.parse('http://[::1]:3000')
puts uri.host #=> '[::1]'
puts uri.hostname #=> '::1'
Net::HTTP.start(uri.host, uri.port) do |http|
http.get('/')
end #=> getaddrinfo: Name or 2 vice not known (SocketError)
require 'date'
describe Date do
it do
Date.stub(:today) { 'today' }
Date.today.should == 'today'
end
end
class FizzBuzz
def self.say(num)
str = [[:Fizz][num%3], [:Buzz][num%5]].join('')
str.empty? ? num : str
end
end
describe FizzBuzz do
subject { described_class.say(num) }
context 'with 1' do