zucker 0.9 → 1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +2 -2
- data/lib/zucker.rb +6 -1
- data/lib/zucker/1/{descriptions → desc}/D.yaml +2 -4
- data/lib/zucker/1/{descriptions → desc}/aliases.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/array.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/array2proc.yaml +2 -4
- data/lib/zucker/1/{descriptions → desc}/binding.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/blank.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/class2proc.yaml +2 -5
- data/lib/zucker/1/{descriptions → desc}/egonil.yaml +2 -5
- data/lib/zucker/1/{descriptions → desc}/enumerable.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/hash.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/hash2proc.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/iterate.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/ivars.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/kernel.yaml +0 -0
- data/lib/zucker/1/{descriptions → desc}/mcopy.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/mm.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/regexp2proc.yaml +1 -3
- data/lib/zucker/1/{descriptions → desc}/sandbox.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/square_brackets_for.yaml +0 -2
- data/lib/zucker/1/{descriptions → desc}/string.yaml +0 -2
- data/lib/zucker/1/desc/unary.yaml +15 -0
- data/lib/zucker/1/{descriptions → desc}/union.yaml +0 -0
- data/lib/zucker/1/{documentation → doc}/meta.yaml +0 -0
- data/lib/zucker/1/{documentation → doc}/zucker_doc.html +732 -604
- data/lib/zucker/1/doc/zucker_doc.rb +493 -0
- data/lib/zucker/1/hash2proc.rb +2 -0
- data/lib/zucker/1/{specification → spec}/D_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/aliases_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/array2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/array_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/binding_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/blank_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/class2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/egonil_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/enumerable_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/hash2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/hash_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/iterate_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/ivars_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/kernel_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/mcopy_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/mm_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/regexp2proc_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/sandbox_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/square_brackets_for_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/string_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/unary_spec.rb +0 -0
- data/lib/zucker/1/{specification → spec}/union_spec.rb +0 -0
- data/lib/zucker/D.rb +19 -0
- data/lib/zucker/aliases.rb +28 -0
- data/lib/zucker/all.rb +5 -1
- data/lib/zucker/array.rb +17 -0
- data/lib/zucker/array2proc.rb +10 -0
- data/lib/zucker/binding.rb +25 -0
- data/lib/zucker/blank.rb +26 -0
- data/lib/zucker/class2proc.rb +10 -0
- data/lib/zucker/debug.rb +3 -1
- data/lib/zucker/default.rb +24 -1
- data/lib/zucker/desc/D.yaml +23 -0
- data/lib/zucker/desc/aliases.yaml +16 -0
- data/lib/zucker/desc/array.yaml +14 -0
- data/lib/zucker/desc/array2proc.yaml +13 -0
- data/lib/zucker/desc/binding.yaml +10 -0
- data/lib/zucker/desc/blank.yaml +12 -0
- data/lib/zucker/desc/class2proc.yaml +12 -0
- data/lib/zucker/desc/egonil.yaml +17 -0
- data/lib/zucker/desc/enumerable.yaml +11 -0
- data/lib/zucker/desc/hash.yaml +12 -0
- data/lib/zucker/desc/hash2proc.yaml +11 -0
- data/lib/zucker/desc/iterate.yaml +17 -0
- data/lib/zucker/desc/ivars.yaml +20 -0
- data/lib/zucker/desc/kernel.yaml +13 -0
- data/lib/zucker/desc/mcopy.yaml +12 -0
- data/lib/zucker/desc/mm.yaml +12 -0
- data/lib/zucker/desc/regexp2proc.yaml +13 -0
- data/lib/zucker/desc/sandbox.yaml +14 -0
- data/lib/zucker/desc/square_brackets_for.yaml +23 -0
- data/lib/zucker/desc/string.yaml +13 -0
- data/lib/zucker/desc/unary.yaml +15 -0
- data/lib/zucker/desc/union.yaml +11 -0
- data/lib/zucker/doc/meta.yaml +4 -0
- data/lib/zucker/doc/zucker_doc.html +1286 -0
- data/lib/zucker/doc/zucker_doc.rb +493 -0
- data/lib/zucker/egonil.rb +14 -0
- data/lib/zucker/enumerable.rb +12 -0
- data/lib/zucker/hash.rb +19 -0
- data/lib/zucker/hash2proc.rb +14 -0
- data/lib/zucker/iterate.rb +23 -0
- data/lib/zucker/ivars.rb +26 -0
- data/lib/zucker/kernel.rb +20 -0
- data/lib/zucker/mcopy.rb +9 -0
- data/lib/zucker/mm.rb +31 -0
- data/lib/zucker/regexp2proc.rb +10 -0
- data/lib/zucker/sandbox.rb +15 -0
- data/lib/zucker/spec/D_spec.rb +27 -0
- data/lib/zucker/spec/aliases_spec.rb +34 -0
- data/lib/zucker/spec/array2proc_spec.rb +8 -0
- data/lib/zucker/spec/array_spec.rb +23 -0
- data/lib/zucker/spec/binding_spec.rb +6 -0
- data/lib/zucker/spec/blank_spec.rb +20 -0
- data/lib/zucker/spec/class2proc_spec.rb +9 -0
- data/lib/zucker/spec/egonil_spec.rb +30 -0
- data/lib/zucker/spec/enumerable_spec.rb +8 -0
- data/lib/zucker/spec/hash2proc_spec.rb +12 -0
- data/lib/zucker/spec/hash_spec.rb +10 -0
- data/lib/zucker/spec/iterate_spec.rb +54 -0
- data/lib/zucker/spec/ivars_spec.rb +20 -0
- data/lib/zucker/spec/kernel_spec.rb +34 -0
- data/lib/zucker/spec/mcopy_spec.rb +13 -0
- data/lib/zucker/spec/mm_spec.rb +6 -0
- data/lib/zucker/spec/regexp2proc_spec.rb +9 -0
- data/lib/zucker/spec/sandbox_spec.rb +7 -0
- data/lib/zucker/spec/square_brackets_for_spec.rb +59 -0
- data/lib/zucker/spec/string_spec.rb +29 -0
- data/lib/zucker/spec/unary_spec.rb +26 -0
- data/lib/zucker/spec/union_spec.rb +13 -0
- data/lib/zucker/square_brackets_for.rb +20 -0
- data/lib/zucker/string.rb +25 -0
- data/lib/zucker/unary.rb +22 -0
- data/lib/zucker/union.rb +14 -0
- metadata +119 -51
- data/lib/zucker/1/descriptions/unary.yaml +0 -15
- data/lib/zucker/1/documentation/zucker_doc.rb +0 -369
data/lib/zucker/hash.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
class Hash
|
2
|
+
def self.zip(keys,values)
|
3
|
+
Hash[ *[1,2,3].zip( [4,5,6] ).flatten ]
|
4
|
+
end
|
5
|
+
|
6
|
+
def <<(other)
|
7
|
+
case
|
8
|
+
when other.is_a?(Hash)
|
9
|
+
merge! other
|
10
|
+
when other.respond_to?(:to_splat)
|
11
|
+
merge! Hash[*other]
|
12
|
+
else
|
13
|
+
raise TypeError, 'can only append other Hashs and Enumerables'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# J-_-L
|
19
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
def iterate(*params)
|
2
|
+
# params.shift.zip(*params).each{ |*elements| yield *elements }
|
3
|
+
|
4
|
+
first = params.shift
|
5
|
+
if params.empty? # single param - like each
|
6
|
+
if block_given?
|
7
|
+
first.map{|e| yield e }
|
8
|
+
else
|
9
|
+
first.map
|
10
|
+
end
|
11
|
+
else
|
12
|
+
padded_first = Array.new( [first, *params].max_by(&:size).size ){|i| first[i] } # append nils
|
13
|
+
obj = padded_first.zip *params
|
14
|
+
if block_given?
|
15
|
+
obj.map{|es| yield *es }
|
16
|
+
else
|
17
|
+
obj.map.to_enum
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# J-_-L
|
23
|
+
|
data/lib/zucker/ivars.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
def instance_variables_from(obj, *only)
|
2
|
+
iter =
|
3
|
+
if obj.is_a? Binding
|
4
|
+
obj.eval('local_variables').map{|e| [obj.eval(e), e] }
|
5
|
+
elsif obj.is_a? Hash
|
6
|
+
obj.map{|k,v| [v,k] }
|
7
|
+
else
|
8
|
+
# elsif obj.is_a? Enumerable
|
9
|
+
obj.each.with_index
|
10
|
+
end
|
11
|
+
|
12
|
+
ret = []
|
13
|
+
iter.each{ |value, arg|
|
14
|
+
arg = arg.to_s
|
15
|
+
if only.include?(arg) || only.include?(arg.to_sym) || only.empty?
|
16
|
+
arg = '_' + arg if (48..57).member? arg.unpack('C')[0] # 1.8+1.9
|
17
|
+
ret << ivar = :"@#{arg}"
|
18
|
+
self.instance_variable_set ivar, value
|
19
|
+
end
|
20
|
+
}
|
21
|
+
ret
|
22
|
+
end
|
23
|
+
alias ivars instance_variables_from
|
24
|
+
|
25
|
+
# J-_-L
|
26
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
def activate_warnings!
|
2
|
+
$VERBOSE = true
|
3
|
+
end
|
4
|
+
|
5
|
+
def deactivate_warnings!
|
6
|
+
$VERBOSE = false
|
7
|
+
end
|
8
|
+
|
9
|
+
def warnings_activated?
|
10
|
+
$VERBOSE
|
11
|
+
end
|
12
|
+
|
13
|
+
def library?
|
14
|
+
__FILE__ != $PROGRAM_NAME
|
15
|
+
end
|
16
|
+
|
17
|
+
def ignore_sigint! # ctrl+c
|
18
|
+
Signal.trap *%w|SIGINT IGNORE|
|
19
|
+
end
|
20
|
+
|
data/lib/zucker/mcopy.rb
ADDED
data/lib/zucker/mm.rb
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
module Kernel
|
2
|
+
def method_list(levels = 1)
|
3
|
+
if self.is_a? Module
|
4
|
+
klass, method_function = self, :public_methods
|
5
|
+
else
|
6
|
+
klass, method_function = self.class, :public_instance_methods
|
7
|
+
|
8
|
+
eigen = self.singleton_methods
|
9
|
+
if !eigen.empty?
|
10
|
+
puts :Eigenclass # sorry for not being up to date, I just love the word
|
11
|
+
p self.singleton_methods
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
levels.times{ |level|
|
16
|
+
if cur = klass.ancestors[level]
|
17
|
+
p cur # put class name
|
18
|
+
p cur.send method_function, false # put methods of the class
|
19
|
+
else
|
20
|
+
break
|
21
|
+
end
|
22
|
+
}
|
23
|
+
|
24
|
+
self # or whatever
|
25
|
+
end
|
26
|
+
|
27
|
+
alias mm method_list
|
28
|
+
end
|
29
|
+
|
30
|
+
# J-_-L
|
31
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'D'
|
2
|
+
|
3
|
+
describe 'Object#D' do
|
4
|
+
|
5
|
+
let :a do
|
6
|
+
[ 1, "1", 2..5, [], {:hallo => :du}, nil, true ]
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should not change the object"s value' do
|
10
|
+
a.each{ |e|
|
11
|
+
(e.D).should == e
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should puts .inspect if no block is given (and not change the object's value)" do
|
16
|
+
# a.each{ |e|
|
17
|
+
# (e.D).should == e
|
18
|
+
# }
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should puts the block if it is given (and not change the object's value)" do
|
22
|
+
a.each{ |e|
|
23
|
+
(e.D{|value| "This is a: #{value}"}).should == e
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'aliases'
|
2
|
+
|
3
|
+
describe '(aliases)' do
|
4
|
+
it 'should create these aliases [ sorry, no real spec for this one :P ] ' do
|
5
|
+
# alias is_an? is_a?
|
6
|
+
|
7
|
+
# module Enumerable
|
8
|
+
# alias with zip
|
9
|
+
# alias % zip
|
10
|
+
# end
|
11
|
+
|
12
|
+
# class Array
|
13
|
+
# alias ** product
|
14
|
+
# end
|
15
|
+
|
16
|
+
# class Hash
|
17
|
+
# alias + merge
|
18
|
+
# end
|
19
|
+
|
20
|
+
# class Binding
|
21
|
+
# #alias [] eval
|
22
|
+
# def [](expr)
|
23
|
+
# self.eval "#{expr}"
|
24
|
+
# end
|
25
|
+
# end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should define these constants' do
|
30
|
+
Infinity.finite?.should == false
|
31
|
+
NaN.nan?.should == true
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'array'
|
2
|
+
|
3
|
+
describe 'Array#^' do
|
4
|
+
it 'should do an exclusive or' do
|
5
|
+
a = [1,2,3,4]
|
6
|
+
b = [3,4,5,6]
|
7
|
+
(a^b).should == [1,2,5,6]
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe 'Array#sum' do
|
12
|
+
it 'should sum the array' do
|
13
|
+
[1,2,3,4,5].sum.should == 15
|
14
|
+
%w|More Ruby|.sum.should == 'MoreRuby'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'Array#chrs' do
|
19
|
+
it 'should convert the array to a string, using each element as ord value for the char' do
|
20
|
+
[72, 97, 108, 108, 111].chrs.should == 'Hallo'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'blank'
|
2
|
+
|
3
|
+
describe 'Object#blank?' do
|
4
|
+
it 'should be blank for blank values' do
|
5
|
+
blank_values = [ nil, false, '', ' ', " \n\t \r ", [], {}, // ]
|
6
|
+
|
7
|
+
blank_values.each{ |blank|
|
8
|
+
blank.blank?.should == true
|
9
|
+
}
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'should not be blank for non blank values' do
|
13
|
+
present_values = [ Object.new, true, 0, 1, 'a', [nil], { nil => nil } ]
|
14
|
+
|
15
|
+
present_values.each{ |present|
|
16
|
+
present.blank?.should == false
|
17
|
+
}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'egonil'
|
2
|
+
|
3
|
+
describe 'egonil' do
|
4
|
+
it 'should not raise nil exceptions in the block' do
|
5
|
+
proc do
|
6
|
+
egonil{ nil.some_methods.that[:do].not.exist }
|
7
|
+
end.should_not raise_exception
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'should return the nil_value if given' do
|
11
|
+
egonil(9){ nil.some_methods.that[:do].not.exist }.should == 9
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should restore default behaviour after the block' do
|
15
|
+
proc do
|
16
|
+
egonil{ nil.some_methods.that[:do].not.exist }
|
17
|
+
end.should_not raise_exception
|
18
|
+
|
19
|
+
proc do
|
20
|
+
nil.a_method
|
21
|
+
end.should raise_exception NoMethodError
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'raise NoMethodError for non-nil objects' do
|
25
|
+
proc do
|
26
|
+
egonil{ 5.a_method }
|
27
|
+
end.should raise_exception NoMethodError
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'hash2proc'
|
2
|
+
|
3
|
+
describe 'Hash#to_proc' do
|
4
|
+
it 'should run the proc given in the value for a key in the hash' do
|
5
|
+
[1,2,3,4].map(&{
|
6
|
+
4 => :to_s,
|
7
|
+
# 3 => [:to_s, 2] # "11" => if array2proc is used
|
8
|
+
2 => lambda {|e| e + 1000}
|
9
|
+
}).should == [1, 1002, 3, "4"]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'iterate'
|
2
|
+
|
3
|
+
describe 'Object#iterate' do
|
4
|
+
let :a do [1, 2, 3] end
|
5
|
+
let :b do %w|a b c d| end
|
6
|
+
let :res do Hash.new {[]} end
|
7
|
+
|
8
|
+
it 'should behave like Enumerable#each for a single argument' do
|
9
|
+
iterate a do |ele|
|
10
|
+
res[:iter] << ele
|
11
|
+
end
|
12
|
+
|
13
|
+
a.each do |ele|
|
14
|
+
res[:each] << ele
|
15
|
+
end
|
16
|
+
|
17
|
+
res[:iter].should == res[:each]
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'should pass the right params to the block' do
|
21
|
+
res = Hash.new {[]} # TODO: why?
|
22
|
+
res[:iter_a_b] = [] # ....
|
23
|
+
res[:iter_b_a] = [] # ....
|
24
|
+
|
25
|
+
|
26
|
+
iterate a, b do |e,f|
|
27
|
+
res[:iter_a_b] << [e, f]
|
28
|
+
# p res[:iter_a_b], e, f
|
29
|
+
end
|
30
|
+
|
31
|
+
res[:iter_a_b].should == [
|
32
|
+
[1, 'a'],
|
33
|
+
[2, 'b'],
|
34
|
+
[3, 'c'],
|
35
|
+
[nil, 'd'],
|
36
|
+
]
|
37
|
+
|
38
|
+
iterate b, a do |e,f|
|
39
|
+
res[:iter_b_a] << [e, f]
|
40
|
+
end
|
41
|
+
|
42
|
+
res[:iter_b_a].should == [
|
43
|
+
['a', 1],
|
44
|
+
['b', 2],
|
45
|
+
['c', 3],
|
46
|
+
['d', nil],
|
47
|
+
]
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
it 'should return enumerators if no block is applied' do
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|