Skip to content

Instantly share code, notes, and snippets.

View pasberth's full-sized avatar

pasberth pasberth

View GitHub Profile
@pasberth
pasberth / errnum.rb
Created August 17, 2011 13:22
errNNN というプレフィックスをつけて数字を呼びます。 err351 が 3510000000000 を返します。
# -*- coding: utf-8 -*-
def method_missing(funcname, *args)
if funcname =~ /^err([0-9]+)$/
ret = $1.to_i
if $1.length < 13
ret *= 10 ** (13 - $1.length)
end
ret
else
super
@pasberth
pasberth / match.rb
Created August 20, 2011 12:58
case-when のようなものを作ってみた
# -*- coding: utf-8 -*-
module Kernel
def match(obj, &blk)
b = binding
def b.if_that_is *something, &doing
unless @matchers
@matchers = {}
@matchers.default = proc { nil }
end
@pasberth
pasberth / send.js
Created August 24, 2011 17:05
send とか getattr とかを js でしてみる
function getattr(self, attrname) {
return (function() {
return eval("this." + attrname);
}).apply(self, arguments);
}
function send(self, funcname) {
return (function() {
return eval("this." + funcname + "(arguments);");
}).apply(self, arguments);
}
@pasberth
pasberth / find.rb
Created August 25, 2011 14:39
find コマンドみたいなの
# -*- coding: utf-8 -*-
# usage: ruby find.rb ~ --name .* --depth 1
# need to OptParser
require "optparser"
$filename = nil
$rootdir = File.expand_path '.'
$recursive = true
OptParser.add_option('--name') { |filename|
$filename = filename
@pasberth
pasberth / class.rb
Created August 26, 2011 14:01
Class を使わないでオブジェクト指向っぽいことをする
module Kernel
def object &definition
# binding
attrs = {}
# object
obj = proc { |attr_name|
{ :self => obj,
:dict => function(obj) { |this| this.dict },
:set => function(obj) { |this, attr_name, value| this.dict[attr_name.to_sym] = value },
@pasberth
pasberth / apply.rb
Created August 26, 2011 17:06
apply.rb - Proc を binding を変えて実行します
class Proc
def apply(binding, *args)
binding.instance_variable_set :@args, args
binding.instance_eval do
def method_missing varname, *args, &blk
return if varname == :to_ary
if !args.empty? || blk
$temp_args = args
$temp_blk = blk
res = eval "#{varname} *$temp_args, &$temp_blk"
@pasberth
pasberth / r.rb
Created August 27, 2011 15:53
Python でこういうことをしたいっていう願望
# Python でもこんな感じのものを定義して
# ["<a>" + e + "</a>" for e in r(['root1', ['branch0', 'branch1'], 'root2', ['branches'] ])]
# って書きたい
class Array
def r &blk
[].tap { |res|
each { |e|
if e.is_a? Array
res << e.r(&blk)
@pasberth
pasberth / anonymous_class.rb
Created August 31, 2011 13:11
これで Ruby でも Java の匿名クラスみたいに書ける!!!
module Kernel
def method_missing(klass_name, *args, &definition)
klass = (self.is_a?(Module) ? self : self.class)
begin
const = klass.const_get(klass_name)
if const.instance_of? Class
define_singleton_method(klass_name) do |*args, &definition|
obj = const.new *args
obj.instance_eval(&definition || proc {})
@pasberth
pasberth / endlessruby.rb
Created September 1, 2011 10:57
Ruby の end をなくす!!!!
# -*- coding: utf-8 -*-
module EndlessRuby
def blank_line? line
return true unless line
(line.chomp.gsub /\s+?/, '') == ""
end
def indent line, level, indent=" "
"#{indent * level}#{line}"
@pasberth
pasberth / nekousa_theorem.er
Created September 9, 2011 15:51
とりあえずやってみた
#!/usr/local/bin/ruby
# -*- coding: utf-8 -*-
"
問題:
 ミミは5つあります。ウサミミは3つ、ネコミミは2つです
 テトラちゃんと、ミルカさんと、ユーリは、このうちいずれかのミミを頭に付けています。余りはふたつです。
 自分のミミは見えません。
 他のふたりのミミは見えます。
 余りのふたつのミミをなんだか知ることはできません。