Last active
August 29, 2015 14:14
-
-
Save lnznt/144397907aac9ec77f87 to your computer and use it in GitHub Desktop.
My Extension (Ruby)
This file contains hidden or 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
module ArrayTraversable | |
def traverse(each=:each, *args, &block) | |
visit = -> y, obj=self, idx=[] do | |
idx.push 0 | |
obj.send(each, *args) do |x| | |
if x.kind_of?(Array) | |
visit.(y, x, idx) | |
else | |
y << [x, idx] | |
end | |
idx[-1] += 1 | |
end | |
idx.pop | |
end | |
enum = Enumerator.new &visit | |
block ? enum.send(each, &block) : enum | |
end | |
def traverse!(map=:map, *args, &block) | |
traverse map, *args, &block | |
end | |
end | |
class Array | |
include ArrayTraversable | |
end | |
if $0 == __FILE__ | |
ary = [1,[[2,[[[3],[[[4]]]]]],5]] | |
enum = ary.traverse | |
p enum.next | |
p enum.next | |
p enum.next | |
p enum.next | |
p enum.next | |
enum.rewind | |
p enum.next | |
puts "-" * 8 | |
ary.traverse {|x,| puts x } # 1 | |
# 2 | |
# 3 | |
# 4 | |
# 5 | |
p ary.traverse.map {|x,| x.to_s } #=> ["1", [["2", [[["3"], [[["4"]]]]]], "5"]] | |
end |
This file contains hidden or 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 Binding | |
alias [] local_variable_get | |
alias []= local_variable_set | |
end |
This file contains hidden or 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
module Hook | |
def hook(&block) | |
module_eval { prepend Module.new &block } | |
end | |
end | |
class Class | |
include Hook | |
end |
This file contains hidden or 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
module KindOfTest | |
def true? ; true_class? ; end | |
def false? ; false_class? ; end | |
def respond_to?(sym, *) | |
kind_of_test?(sym) || super | |
end | |
private | |
def respond_to_missing?(sym, *) | |
kind_of_test?(sym) || super | |
end | |
def method_missing(sym, *) | |
return kind_of?(kind_of_test_class(sym)) if kind_of_test?(sym) | |
super | |
end | |
def kind_of_test?(sym) | |
!!kind_of_test_class_name(sym) | |
end | |
def kind_of_test_class(sym) | |
eval kind_of_test_class_name(sym) | |
end | |
def kind_of_test_class_name(sym) | |
return 'ARGF.class' if sym == :_ARGF_class? | |
return unless sym =~ /\?\z/ | |
conv = -> s { s.split('_').map(&:capitalize).join } | |
name = (sym.to_s.chop).split('__').map(&conv) * '::' | |
name if (self.class.const_defined? name) && ((eval name).class == Class) | |
end | |
public | |
def array?(&test) | |
kind_of?(Array) && (!test || all?(&test)) | |
end | |
def tuple?(n=2) | |
kind_of?(Array) && (size == n) | |
end | |
def byte? | |
kind_of?(Integer) && (0..255).include?(self) | |
end | |
end | |
class Object | |
include KindOfTest | |
end |
This file contains hidden or 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 'forwardable' | |
class Object | |
extend Forwardable | |
attr_accessor :log | |
attr_writer :logger | |
def logger ; @logger ||= Logger.new(log) ; end | |
delegate %i(debug info warn error fatal) => :logger | |
end |
This file contains hidden or 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 'logger' | |
module LoggerExt | |
def level_syms | |
Logger::SEV_LABEL.map &:to_sym | |
end | |
def level_sym | |
level_syms[level] | |
end | |
def level_sym?(x) | |
level_syms.include?(x) | |
end | |
def level=(x) | |
super(level_syms.index(x) || x) | |
end | |
def initialize(logdev=nil, *) | |
super | |
end | |
end | |
class Logger | |
prepend LoggerExt | |
end |
This file contains hidden or 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 'binding_ext' | |
require 'string_ext' | |
require 'proc_ext' | |
require 'logger_ext' | |
require 'socket_ext' | |
#require 'logger_delegate' | |
require 'null' | |
require 'hook' | |
require 'kind_of_test' | |
require 'array_traversable' | |
#require 'notifier' | |
require 'instance_property' # gem | |
class Module | |
extend InstanceProperty | |
end | |
require 'object_cascadable' # gem | |
class Object | |
include ObjectCascadable | |
end | |
require 'camelizable' # gem | |
class String | |
include Camelizable | |
end |
This file contains hidden or 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
module Notifier | |
def on(event, &block) | |
listeners_list_select(event) << block | |
end | |
def off(event, &block) | |
listeners_list_select(event).delete_if &block | |
end | |
def notify(event, &block) | |
listeners_list_select(event).each &(block || :call) | |
end | |
def listeners_lists | |
@listeners_lists ||= Hash.new {|h,k| h[k] = [] } | |
end | |
attr_writer :listeners_list_selector | |
def listeners_list_selector(&block) | |
@listeners_list_selector = block if block | |
@listeners_list_selector ||= -> ll, arg, * { ll[arg] || -> * {} } | |
end | |
def listeners_list_select(*args) | |
listeners_list_selector.(listeners_lists, *args) | |
end | |
end | |
if __FILE__ == $0 | |
class C | |
include Notifier | |
def foo | |
notify :foo | |
end | |
def bar(*args) | |
notify(:bar) {|x| x.(*args) } | |
end | |
end | |
c = C.new | |
c.on(:foo) { puts 'This is foo' } | |
px = -> { puts 'This is foo 2' } | |
c.on :foo, &px | |
c.on(:bar) {|x| puts "This is bar: #{x}" } | |
c.foo | |
c.bar 'hello' | |
c.off(:foo) {|x| x == px } | |
c.foo | |
end |
This file contains hidden or 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
# | |
# Null Object Design Pattern | |
# | |
require 'singleton' | |
class Null | |
include Singleton | |
def null? | |
true | |
end | |
def respond_to?(*) | |
true | |
end | |
private | |
def respond_to_missing?(*) | |
true | |
end | |
def method_missing(*) ; end | |
end | |
$null = Null.instance |
This file contains hidden or 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
module Functional | |
def *(other) | |
-> *a, &b { other.(call *a, &b) } | |
end | |
def args(*args) | |
-> *a, &b { call *args, *a, &b } | |
end | |
def argsr(*args) | |
-> *a, &b { call *a, *args, &b } | |
end | |
end | |
class Proc | |
include Functional | |
def self.Z(f) | |
-> x { f.(-> y {x.(x).(y)}) } | |
.(-> x { f.(-> y {x.(x).(y)}) }) | |
end | |
end |
This file contains hidden or 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 'socket' | |
module TcpServerLoopExt | |
def tcp_server_loop(*args) | |
return enum_for(:tcp_server_loop, *args) unless block_given? | |
super | |
end | |
def tcp_server_thread_loop(*args) | |
tcp_server_loop(*args) do |conn, addr| | |
Thread.new do | |
begin | |
yield conn, addr | |
ensure | |
conn.close | |
end | |
end | |
end | |
end | |
end | |
class Socket | |
class << self | |
prepend TcpServerLoopExt | |
end | |
end |
This file contains hidden or 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
module StringExt | |
def remove(re) | |
gsub(re) {} | |
end | |
def wrap(w) | |
"#{w[0]}#{self}#{w[-1]}" | |
end | |
def indent(n, s=' ') | |
"#{s * n}#{self}" | |
end | |
def pickup(re) | |
scan(/\A.*?#{re}.*?\z/).flatten | |
end | |
alias - remove | |
alias ** wrap | |
alias >> indent | |
alias ^ pickup | |
end | |
class String | |
include StringExt | |
end | |
if __FILE__ == $0 | |
p "AB-C-AB" - "AB" # => "-C-" | |
p "AB-C-AB" - "C-AB" # => "AB-" | |
p "AB-BB-AB" - /^AB/ # => "-BB-AB" | |
p "AB-BB-AB" - /AB$/ # => "AB-BB-" | |
p "AB-BB-AB" - /AB/ # => "-BB-" | |
p "hello" ** "()" # => "(hello)" | |
p "hello" ** "|" # => "|hello|" | |
p "hello" ** "{...}" # => "{hello}" | |
p "hello" ** "->" # => "-hello>" | |
p "hello" ** ['(',')'] # => "(hello)" | |
p "hello" ** ['|'] # => "|hello|" | |
p "hello" ** ['{',nil,'}'] # => "{hello}" | |
p "hello" ** ['-','>'] # => "-hello>" | |
red = ["\e[31m", "\e[m"] | |
p "hello" ** red # => "\e[31mhello\e[m" | |
p "hello" >> 2 # => " hello" | |
p "hello".indent 2, ">" # => ">>hello" | |
p "hello".indent 2, "->" # => "->->hello" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment