Skip to content

Instantly share code, notes, and snippets.

@maraigue
maraigue / gist:2802312
Created May 27, 2012 05:50
Rubyのrescue演算子に例外クラスが指定できないので無理やり指定できるようにしてみた(その1)
# その2: http://gist.github.com/2802352
# Rubyでは open("nonexistent-file.txt") rescue nil のような形で
# rescue付きの式を書けるが、この際はbegin~rescueを書く場合と異なり
# 例外クラスを指定することができない!
def if_(error_class, value)
if $!.kind_of?(error_class)
value
else
@maraigue
maraigue / triangle.rb
Created April 24, 2012 18:34
[Ruby] 三角形の外心・重心・内心・垂心を計算するプログラム ※ブラウザで動く版 http://hhiro.net/triangle/
#!/usr/bin/env ruby
# 三角形の外心・重心・内心・垂心を計算するプログラム
# (C) 2012 H.Hiro(Maraigue) <main[at]hhiro.net>
# MIT Licenseの元で自由に利用可
require "matrix"
# 頂点を表すクラス
class Point
@maraigue
maraigue / test.rb
Created January 12, 2012 07:06
[Ruby] Enumerable#grepの補集合
require "./vgrep.rb"
p ["foo", "bar", "buz"].grep(/b/) # => ["bar", "buz"]
p ["foo", "bar", "buz"].vgrep(/b/) # => ["foo"]
p ["foo", "bar", "buz"].vgrep(/b/){ |x| x.upcase } # => ["FOO"]
@maraigue
maraigue / increment_cast_pointer.cpp
Created January 8, 2012 10:13
必要あって size_t hoge; (reinterpret_cast<int *>(hoge))++; 的なことをしたかったけどこの記法じゃ無理だったので
#include <iostream>
int main(void){
size_t hoge;
hoge = 0;
// 意図としてはこう書きたいんだけど無理
// (reinterpret_cast<int *>(hoge))++;
// でもここまで愚直に書くのも嫌なので
@maraigue
maraigue / benchmark.cpp
Created December 30, 2011 12:51
[C++][Linux] clock関数でベンチマークを取ろうとしたのだが、結果が測定箇所以外に書かれたコードに大幅に依存してしまう
#include <iostream>
#include <ctime>
long long int factorial(long long int x){
if(x <= 1) return 1;
return(x * factorial(x - 1));
}
int main(void){
clock_t sum1 = 0, sum2 = 0, begin, end;
@maraigue
maraigue / any_type_haveing.cpp
Created December 12, 2011 20:40
[C++] いくつかの特定の型のみを引数に取れる関数を定義する
#include <iostream>
// ------------------------------------------------------------
// いくつかの特定の型のみを引数に取れる関数を定義する
// ------------------------------------------------------------
// [動機]
// 「引数にはクラスAかクラスBのインスタンスのみ渡せる」ような
// 関数を定義するにあたって、
// ●どちらのクラスのインスタンスが来ても処理はほぼ同じな上、
// そんな関数が多数あるので、単純なオペレータオーバーロードには
@maraigue
maraigue / export-permission.sh
Created December 4, 2011 09:53
To export permissions of all files under current directory
#!/bin/sh
# To export permissions of all files under current directory
# (for make a backup from an HDD of Un*x file system to of Windows file system)
# カレントディレクトリ以下のすべてのファイルについて、パーミッションを書き出す
# (Un*xのファイルシステムからWindowsのファイルシステムへバックアップを作るときなどに)
find . -exec stat -c 'chmod %a %n' \{\} \;
@maraigue
maraigue / fundoshi.cpp
Created November 17, 2011 06:19
fundoshi.hpp: すでにconstな文字列が確保されているメモリ上の領域に対し、その部分文字列を、メモリを共有したまま別の文字列のごとく扱うためのクラス ※要:C++11(C++0x)対応コンパイラ / こちらもどうぞ https://github.com/maraigue/fundoshi.hpp
#ifndef _FUNDOSHI_CPP_
#define _FUNDOSHI_CPP_
#include "fundoshi.hpp"
namespace fundoshi{
template <class CharType>
size_t strlen(const CharType * str){
size_t result = 0;
@maraigue
maraigue / FixnumBignum.rb
Created October 21, 2011 08:23
Fixnum? Bignum?
#!/usr/bin/ruby
# run with Ruby1.8.6, Ruby1.9.2
x = 1234567890
y = 1000000000
x.class #=> Bignum
y.class #=> Fixnum
(x - y).class #=> Fixnum
@maraigue
maraigue / dup_if.rb
Created October 17, 2011 08:24
[Ruby] dup_if - dupできるオブジェクトは複製し、そうでなければselfを返すメソッド
class Object
def dup_if
begin
self.dup
rescue TypeError
self
end
end
def clone_if