Skip to content

Instantly share code, notes, and snippets.

#
# extension for rspec
# this extension enables to check should_receive in stub_chain
# you must require this file in 'spec/spec_helper.rb'
#
#
# Example:
# in controller,
#
# def index
@masarakki
masarakki / auto_define_mock_method
Created October 8, 2010 03:55
auto mock_xxxx hack
#
# write it in the bottom of spec/spec_helper.rb
#
def method_missing(name, *args)
if matched = name.to_s.match(/mock_(.*)/)
klass = matched[1].classify.constantize
var_name = "@#{name.to_s}"
self.class.send(:define_method, name) do |*stubs|
stubs = stubs.try(:first) || {}
instance_variable_get(var_name) ||
@masarakki
masarakki / sample_of_alias_class_method.rb
Created December 2, 2010 08:28
HOW TO alias class method in ruby
class BaseClass
def self.find
"find"
end
end
# in class definition
class ClassA < BaseClass
def self.find_with_my_name
find_without_my_name + " ClassA"
@masarakki
masarakki / gist:755974
Created December 27, 2010 08:59
ほげほげ
#include <stdio.h>
void main(int argc, char** argv){
int i, a = atoi(argv[1]), b = atoi(argv[2]), sum = 0;
for(i = (a > 0 ? a : 0); i <= b && i <= 100; i ++){
sum += (1 - i % 2) * i;
}
printf("sum = %d\n", sum);
}
@masarakki
masarakki / gist:796740
Created January 26, 2011 14:15
2次画像ダウンローダ
require 'net/http'
require 'uri'
require 'kconv'
Net::HTTP.version_1_2
puts 'require url' and exit if ARGV[0].nil?
@masarakki
masarakki / tsunami_mode.rb
Created April 20, 2011 05:43
search and quick tweet for earthquake.gem
# -*- coding: utf-8 -*-
#
# Earthquakeを実況モードにするプラグインです
# 普通に起動した後
# :tsunami #K-ON #tbs
# のようにして実況モードに入ります
#
# すると #K-ON (空白区切りで最初のキーワードのみ) で検索した結果が
# リアルタイムに更新されます
#
@masarakki
masarakki / miko_fiber.rb
Created April 20, 2011 07:40
sample of fiber
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
miko = Fiber.new do
cnt = 0
puts "みなさーん 元気ですかー!"
Fiber.yield
puts "それでは早速、いってみよー!"
Fiber.yield
@masarakki
masarakki / earthquake_ng_plugin.rb
Created April 21, 2011 17:57
earthquake_ng_plguin
module Earthquake
attr_accessor :ng_list
def self.ng_list
@ng_list ||= []
end
def self.ng_list=(ng_list)
@ng_list = ng_list
end
@masarakki
masarakki / misakura_mode.rb
Created April 23, 2011 20:49
TLがみさくら語になるearthquake.gemのプラグイン
# -*- coding: utf-8 -*-
Earthquake.init do
output :tweet do |item|
next unless item["text"]
info = []
if item["in_reply_to_status_id"]
info << "(reply to #{id2var(item["in_reply_to_status_id"])})"
elsif item["retweeted_status"]
info << "(retweet of #{id2var(item["retweeted_status"]["id"])})"
@masarakki
masarakki / sample_integers.rb
Created May 2, 2011 08:37
add method to Integer to choice integers like array#sample
class Integer
def sample(num = 1)
to_a.sample(num)
end
def to_a
@to_a ||= self.times.map{|i| i + 1}
end
end