This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# その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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# 三角形の外心・重心・内心・垂心を計算するプログラム | |
# (C) 2012 H.Hiro(Maraigue) <main[at]hhiro.net> | |
# MIT Licenseの元で自由に利用可 | |
require "matrix" | |
# 頂点を表すクラス | |
class Point |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
int main(void){ | |
size_t hoge; | |
hoge = 0; | |
// 意図としてはこう書きたいんだけど無理 | |
// (reinterpret_cast<int *>(hoge))++; | |
// でもここまで愚直に書くのも嫌なので |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// ------------------------------------------------------------ | |
// いくつかの特定の型のみを引数に取れる関数を定義する | |
// ------------------------------------------------------------ | |
// [動機] | |
// 「引数にはクラスAかクラスBのインスタンスのみ渡せる」ような | |
// 関数を定義するにあたって、 | |
// ●どちらのクラスのインスタンスが来ても処理はほぼ同じな上、 | |
// そんな関数が多数あるので、単純なオペレータオーバーロードには |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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' \{\} \; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _FUNDOSHI_CPP_ | |
#define _FUNDOSHI_CPP_ | |
#include "fundoshi.hpp" | |
namespace fundoshi{ | |
template <class CharType> | |
size_t strlen(const CharType * str){ | |
size_t result = 0; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Object | |
def dup_if | |
begin | |
self.dup | |
rescue TypeError | |
self | |
end | |
end | |
def clone_if |