Skip to content

Instantly share code, notes, and snippets.

View pasberth's full-sized avatar

pasberth pasberth

View GitHub Profile
# ary = [0, 1, 2, 3]
# ary.shift.foo until ary.empty?
#
# is equal to:
#
# ary = [0, 1, 2, 3]
# ary.featch { |e| e.foo }
class Array
@pasberth
pasberth / shuffle.rb
Created July 18, 2011 02:13
shuffles array method.
# Array#shuffle returns to shuffled self.
# Array#shuffle! destructive shuffles to self.
# [1, 2, 3, 4, 5].shuffle # => [2, 1, 5, 4, 3] (example)
class Array
def shuffle
clone.shuffle!
end
# irb(main):001:0> require './closure'
# => true
# irb(main):002:0> mkclosure 'madohomu'
# => nil
# irb(main):003:0> homu
# madohomu
# => nil
# irb(main):004:0> mado
# NameError: undefined local variable or method `bind' for main:Object
@pasberth
pasberth / to_param.py
Created July 27, 2011 23:07
ディクショナリをgetのパラメタにする
#
# dict = { "key0": "value0", "key1": "value1", "key2": "value2" }
"&".join( [("%s=%s" % (k, v)) for k, v in dict.items()] )
# => 'key2=value2&key1=value1&key0=value0'
@pasberth
pasberth / to_param.rb
Created July 27, 2011 23:10
ハッシュをgetのパラメタにする
# h = { "key0"=>"value0", "key1"=>"value1", "key2"=>"value2" }
h.map { |k, v| "#{k}=#{v}" }.join('&')
# => "key0=value0&key1=value1&key2=value2"
# -*- coding: utf-8 -*-
# importas は Python で言う import MyClass as RenamedMyClass 的なことをできます
# importas Namespace::MyClass, :RenamedMyClass とします
def importas(from, to_name)
Kernel.const_set(to_name, from)
end
=begin
module Namespace
class File; end
@pasberth
pasberth / cddr.rb
Created August 11, 2011 06:18
cddddddddr
# [0, 1, 2, 3].car #=> 0, that as (car '(0 1 2 3))
# [0, 1, 2, 3].cdr #=> [1, 2, 3], that as (cdr '(0 1 2 3))
# [0, 1, 2, 3].cdddr #=> [3], that as (cdr (cdr (cdr '(0 1 2 3))))
class Array
def car
first
end
@pasberth
pasberth / replace_urls.rb
Created August 12, 2011 09:49
URL を置換するだけです
# -*- coding: utf-8 -*-
class String
AVAILABLE_CHARS_IN_URI = [
'\;', '\/', '\?', '\:', '\@', '\&', '\=', '\+', '\$', '\,',
'\-', '\_', '\.', '\!', '\~', '\*', '\\\'', '\(', '\)',
'\%', '0-1', 'a-z', 'A-Z' '\#'
]
REGEXP_OF_URL = /https?\:\/\/[#{AVAILABLE_CHARS_IN_URI.join('')}]+/
def replace_urls &blk
gsub(REGEXP_OF_URL, &blk)
@pasberth
pasberth / githublike.el
Created August 13, 2011 04:17
emacs の色を github っぽくします
(add-to-list 'default-frame-alist '(foreground-color . "rgb:50/50/50"))
(add-to-list 'default-frame-alist '(background-color . "rgb:ff/ff/ff"))
(set-foreground-color "rgb:50/50/50")
(set-background-color "rgb:ff/ff/ff")
(set-face-foreground 'font-lock-comment-face "rgb:90/90/80")
(set-face-italic-p 'font-lock-comment-face t)
(set-face-foreground 'font-lock-string-face "rgb:90/00/00")
(set-face-foreground 'font-lock-keyword-face "black")
(set-face-bold-p 'font-lock-keyword-face t)
(set-face-foreground 'font-lock-function-name-face "rgb:90/00/00")
@pasberth
pasberth / def.rb
Created August 16, 2011 01:06
dynamic define method with ruby 1.8.7
# -*- coding: utf-8 -*-
class Object
def self.define_method(funcname, *params, &blk)
@@methods ||= {}
@@methods[funcname.to_sym] = blk
class_eval(<<DEFINE)
def #{funcname} #{params.join(', ')}
b = binding
def b.method_missing(varname, *args)