Skip to content

Instantly share code, notes, and snippets.

View gist:218520
def get_each_const(const, context=::Object)
const.to_s.split(/::/).inject(context) {|parent, c|
parent.const_get(c)
}
end
@aereal
aereal / gist:218780
Created Oct 26, 2009
Python's 'import' like it
View gist:218780
def import(feature, as={})
ret = require feature
self.class.instance_eval do
as.each do |(orig, dest)|
c = const_get(orig)
remove_const(orig) if const_defined?(orig)
const_set(dest, c)
end
end unless as.empty?
ret
View vsm.rb
#!/usr/bin/env ruby
#
# usage:
# $ git clone git://github.com/motemen/git-vim.git git-vim
# $ ruby vsm.rb git-vim/
VIMDIR = '~/.vim'
SRCDIR = ARGV[0]
require 'pathname'
View gist:344626
/* Shell Color */
.bg-std { background-color: rgb( 51, 51, 49) }
.fg-std { color: rgb(230, 230, 230) }
.fg-black { color: rgb( 51, 51, 49) }
.fg-red { color: rgb(255, 51, 60) }
.fg-green { color: rgb( 40, 204, 76) }
.fg-yellow { color: rgb(224, 230, 57) }
.fg-blue { color: rgb( 46, 122, 230) }
View gist:425353
hash = {:a => 40, :b => 60, :c => 50, :d => 30}
# #sort_by -> #reverse
hash.sort_by {|k, v| v }.reverse
# => [[:b, 60], [:c, 50], [:a, 40], [:d, 30]]
# #sort_by
hash.sort_by {|k, v| -v }
# => [[:b, 60], [:c, 50], [:a, 40], [:d, 30]]
View gist:425357
module Enumerable
def like?(val, op = :===)
any? {|i| i.__send__(op, val) }
end
end
ignore_pattern = [/\.$/, /\.(?:svn|git)$/]
ignore_pattern.include?('..') # => false
ignore_pattern.like?('..') # => true
View gist:428470
class Greeting
def hello; end
private do
def bonjour; end
end
protected do
def guten_morgen; end
end
View gist:434073
class Integer
def english_name(delimiter='-')
if self < 20
english_table[self]
else
ret = english_table[(self / 10) * 10]
(self % 10) == 0 ? ret : ret + delimiter.to_s + english_table[self % 10]
end
end
View gist:435722
class TaniRyoko
def initialize(shout)
@shout = shout
end
def to_s
"#{@shout}! 新谷良子、#{@shout}!!"
end
end
View gist:435727
TaniRyoko = "谷良子"
def new(syntanix, shout=nil)
if shout
puts "#{shout}! 新#{syntanix}, #{shout}!!!"
else
puts "New #{syntanix} って…100万回も言われてます。もうあきましたっ!!ばーかばーか。"
end
end