Skip to content

Instantly share code, notes, and snippets.

@seanchas116
seanchas116 / different_class.rb
Last active August 29, 2015 14:01
A ruby benchmark to simulate method lookup slowdown by dynamic method definition (in Ruby 2.1.2)
require 'benchmark'
class Test
mcount = 1000
mcount.times do |i|
class_eval "def method#{i}; end"
end
@seanchas116
seanchas116 / pubnet-proxy.rb
Last active December 27, 2015 03:39
titech-pubnetに接続中の場合プロキシ環境変数を設定するだけのスクリプト(Macのみ) シェルでeval `ruby pubnet-proxy.rb`する
def get_ssid
airport = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I'
`#{airport}`.split("\n").each do |line|
words = line.split
if words[0] == 'SSID:'
return words[1]
end
end
@seanchas116
seanchas116 / deferred-map.cc
Last active December 23, 2015 06:59
Deferred "map" function for ranges
#include <type_traits>
#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
template <typename TImpl>
class Enumerator
{
@seanchas116
seanchas116 / extension-method-macro.cpp
Last active December 23, 2015 03:59
A set of C++ macros for defining extension methods
#include <vector>
#include <iostream>
#include <type_traits>
#include <boost/preprocessor.hpp>
#define GET_TYPE(n, array) \
BOOST_PP_TUPLE_ELEM(2, 0, BOOST_PP_ARRAY_ELEM(n, array))
#define GET_ARG(n, array) \
BOOST_PP_TUPLE_ELEM(2, 1, BOOST_PP_ARRAY_ELEM(n, array))
class DoNotation
class DSL
def initialize(&block)
@first_lets = []
@proc_sources = []
@variables = Hash[]
@seanchas116
seanchas116 / parslet-arithmetic.rb
Last active December 21, 2015 13:58
parse and calculate arithmetic expressions with Parslet
require 'parslet'
require 'pp'
class Parser < Parslet::Parser
rule(:lparen) { str('(') >> space? }
rule(:rparen) { str(')') >> space? }
rule(:space) { match('\s').repeat(1) }
rule(:space?) { space.maybe }
@seanchas116
seanchas116 / gist:6306349
Last active December 21, 2015 12:29
僕の考えた最弱な言語
x = 5
y = 10
add = (x, y) =>
x + y
end
z = add x, y
times = (i, fn) =>
iteration = (j) =>
switch j < i
@seanchas116
seanchas116 / libc++.rb
Created June 22, 2013 11:58
A libc++ formula for Homebrew Add "-L/usr/local/lib -I/usr/local/lib/c++/v1" to use
require 'formula'
class Libcxx < Formula
homepage 'http://llvm.org/'
url 'http://llvm.org/releases/3.3/libcxx-3.3.src.tar.gz'
sha1 '7bea00bc1031bf3bf6c248e57c1f4e0874c18c04'
def install
cd './lib' do
@seanchas116
seanchas116 / linecount.rb
Created April 28, 2013 15:05
A Ruby script that just counts the number of lines in a given directory
require 'pathname'
def count_lines(pathname)
count = 0
pathname.each_child do |child|
if child.directory?
count = count + count_lines(child)