zucker 1 → 2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +14 -0
- data/Rakefile +14 -4
- data/lib/zucker.rb +0 -4
- data/lib/zucker/2/D.rb +19 -0
- data/lib/zucker/2/aliases.rb +28 -0
- data/lib/zucker/2/all.rb +6 -0
- data/lib/zucker/2/array.rb +17 -0
- data/lib/zucker/2/array2proc.rb +16 -0
- data/lib/zucker/2/binding.rb +25 -0
- data/lib/zucker/2/blank.rb +26 -0
- data/lib/zucker/2/class2proc.rb +10 -0
- data/lib/zucker/2/debug.rb +4 -0
- data/lib/zucker/2/default.rb +25 -0
- data/lib/zucker/2/desc/D.yaml +23 -0
- data/lib/zucker/2/desc/aliases.yaml +16 -0
- data/lib/zucker/2/desc/array.yaml +14 -0
- data/lib/zucker/2/desc/array2proc.yaml +17 -0
- data/lib/zucker/2/desc/binding.yaml +10 -0
- data/lib/zucker/2/desc/blank.yaml +10 -0
- data/lib/zucker/2/desc/class2proc.yaml +12 -0
- data/lib/zucker/2/desc/egonil.yaml +17 -0
- data/lib/zucker/2/desc/enumerable.yaml +11 -0
- data/lib/zucker/2/desc/hash.yaml +14 -0
- data/lib/zucker/2/desc/hash2proc.yaml +11 -0
- data/lib/zucker/2/desc/info.yaml +12 -0
- data/lib/zucker/2/desc/iterate.yaml +17 -0
- data/lib/zucker/2/desc/ivars.yaml +20 -0
- data/lib/zucker/2/desc/kernel.yaml +13 -0
- data/lib/zucker/2/desc/mcopy.yaml +12 -0
- data/lib/zucker/2/desc/mm.yaml +12 -0
- data/lib/zucker/2/desc/regexp2proc.yaml +13 -0
- data/lib/zucker/2/desc/sandbox.yaml +19 -0
- data/lib/zucker/2/desc/square_brackets_for.yaml +23 -0
- data/lib/zucker/2/desc/string.yaml +13 -0
- data/lib/zucker/2/desc/unary.yaml +15 -0
- data/lib/zucker/2/desc/union.yaml +11 -0
- data/lib/zucker/2/doc/zucker_doc.html +1494 -0
- data/lib/zucker/2/doc/zucker_doc.rb +498 -0
- data/lib/zucker/2/egonil.rb +14 -0
- data/lib/zucker/2/enumerable.rb +12 -0
- data/lib/zucker/2/hash.rb +19 -0
- data/lib/zucker/2/hash2proc.rb +14 -0
- data/lib/zucker/2/info.rb +164 -0
- data/lib/zucker/2/iterate.rb +23 -0
- data/lib/zucker/2/ivars.rb +26 -0
- data/lib/zucker/2/kernel.rb +18 -0
- data/lib/zucker/2/mcopy.rb +9 -0
- data/lib/zucker/2/mm.rb +31 -0
- data/lib/zucker/2/regexp2proc.rb +10 -0
- data/lib/zucker/2/sandbox.rb +19 -0
- data/lib/zucker/2/spec/D_spec.rb +35 -0
- data/lib/zucker/2/spec/aliases_spec.rb +13 -0
- data/lib/zucker/2/spec/array2proc_spec.rb +12 -0
- data/lib/zucker/2/spec/array_spec.rb +23 -0
- data/lib/zucker/2/spec/binding_spec.rb +8 -0
- data/lib/zucker/2/spec/blank_spec.rb +20 -0
- data/lib/zucker/2/spec/class2proc_spec.rb +9 -0
- data/lib/zucker/2/spec/egonil_spec.rb +30 -0
- data/lib/zucker/2/spec/enumerable_spec.rb +8 -0
- data/lib/zucker/2/spec/hash2proc_spec.rb +12 -0
- data/lib/zucker/2/spec/hash_spec.rb +17 -0
- data/lib/zucker/2/spec/info_spec.rb +8 -0
- data/lib/zucker/2/spec/iterate_spec.rb +54 -0
- data/lib/zucker/2/spec/ivars_spec.rb +20 -0
- data/lib/zucker/2/spec/kernel_spec.rb +28 -0
- data/lib/zucker/2/spec/mcopy_spec.rb +13 -0
- data/lib/zucker/2/spec/mm_spec.rb +8 -0
- data/lib/zucker/2/spec/regexp2proc_spec.rb +9 -0
- data/lib/zucker/2/spec/sandbox_spec.rb +26 -0
- data/lib/zucker/2/spec/spec_helper.rb +18 -0
- data/lib/zucker/2/spec/square_brackets_for_spec.rb +59 -0
- data/lib/zucker/2/spec/string_spec.rb +29 -0
- data/lib/zucker/2/spec/unary_spec.rb +26 -0
- data/lib/zucker/2/spec/union_spec.rb +15 -0
- data/lib/zucker/2/square_brackets_for.rb +20 -0
- data/lib/zucker/2/string.rb +25 -0
- data/lib/zucker/2/unary.rb +22 -0
- data/lib/zucker/2/union.rb +14 -0
- data/lib/zucker/all.rb +2 -2
- data/lib/zucker/array2proc.rb +7 -1
- data/lib/zucker/debug.rb +3 -3
- data/lib/zucker/default.rb +1 -1
- data/lib/zucker/desc/D.yaml +1 -1
- data/lib/zucker/desc/array.yaml +1 -1
- data/lib/zucker/desc/array2proc.yaml +5 -1
- data/lib/zucker/desc/binding.yaml +2 -2
- data/lib/zucker/desc/blank.yaml +1 -3
- data/lib/zucker/desc/egonil.yaml +1 -1
- data/lib/zucker/desc/enumerable.yaml +1 -1
- data/lib/zucker/desc/hash.yaml +4 -2
- data/lib/zucker/desc/info.yaml +12 -0
- data/lib/zucker/desc/kernel.yaml +2 -2
- data/lib/zucker/desc/mm.yaml +3 -3
- data/lib/zucker/desc/regexp2proc.yaml +1 -1
- data/lib/zucker/desc/sandbox.yaml +6 -1
- data/lib/zucker/desc/string.yaml +1 -1
- data/lib/zucker/desc/unary.yaml +2 -2
- data/lib/zucker/desc/union.yaml +1 -1
- data/lib/zucker/doc/zucker_doc.html +441 -233
- data/lib/zucker/doc/zucker_doc.rb +32 -27
- data/lib/zucker/hash.rb +3 -3
- data/lib/zucker/info.rb +164 -0
- data/lib/zucker/ivars.rb +1 -1
- data/lib/zucker/kernel.rb +2 -4
- data/lib/zucker/sandbox.rb +7 -3
- data/lib/zucker/spec/D_spec.rb +15 -7
- data/lib/zucker/spec/aliases_spec.rb +3 -24
- data/lib/zucker/spec/array2proc_spec.rb +5 -1
- data/lib/zucker/spec/array_spec.rb +1 -1
- data/lib/zucker/spec/binding_spec.rb +4 -2
- data/lib/zucker/spec/blank_spec.rb +1 -1
- data/lib/zucker/spec/class2proc_spec.rb +1 -1
- data/lib/zucker/spec/egonil_spec.rb +1 -1
- data/lib/zucker/spec/enumerable_spec.rb +1 -1
- data/lib/zucker/spec/hash2proc_spec.rb +1 -1
- data/lib/zucker/spec/hash_spec.rb +10 -3
- data/lib/zucker/spec/info_spec.rb +8 -0
- data/lib/zucker/spec/iterate_spec.rb +1 -1
- data/lib/zucker/spec/ivars_spec.rb +1 -1
- data/lib/zucker/spec/kernel_spec.rb +1 -7
- data/lib/zucker/spec/mcopy_spec.rb +1 -1
- data/lib/zucker/spec/mm_spec.rb +4 -2
- data/lib/zucker/spec/regexp2proc_spec.rb +1 -1
- data/lib/zucker/spec/sandbox_spec.rb +22 -3
- data/lib/zucker/spec/spec_helper.rb +18 -0
- data/lib/zucker/spec/square_brackets_for_spec.rb +1 -1
- data/lib/zucker/spec/string_spec.rb +7 -7
- data/lib/zucker/spec/unary_spec.rb +1 -1
- data/lib/zucker/spec/union_spec.rb +8 -6
- metadata +207 -131
- data/lib/zucker/doc/meta.yaml +0 -4
@@ -0,0 +1,19 @@
|
|
1
|
+
class Hash
|
2
|
+
def self.zip(keys,values)
|
3
|
+
Hash[ *keys.zip( values ).flatten ]
|
4
|
+
end
|
5
|
+
|
6
|
+
def <<(other)
|
7
|
+
case
|
8
|
+
when other.is_a?(Hash)
|
9
|
+
merge! other
|
10
|
+
when other.is_a?(Enumerable) || other.respond_to?(:to_splat)
|
11
|
+
merge! Hash[*other]
|
12
|
+
else
|
13
|
+
raise TypeError, 'can only append other Hashs and Enumerables (or Classes that implement to_splat)'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# J-_-L
|
19
|
+
|
@@ -0,0 +1,164 @@
|
|
1
|
+
module Info
|
2
|
+
class << Info
|
3
|
+
# hash like access
|
4
|
+
def [](what)
|
5
|
+
send what
|
6
|
+
end
|
7
|
+
|
8
|
+
# TODO: maybe add a descriptions method
|
9
|
+
end
|
10
|
+
|
11
|
+
module_function
|
12
|
+
|
13
|
+
# current script file
|
14
|
+
def current_file
|
15
|
+
__FILE__
|
16
|
+
end
|
17
|
+
|
18
|
+
def current_file_directory
|
19
|
+
File.dirname(__FILE__)
|
20
|
+
end
|
21
|
+
|
22
|
+
# input
|
23
|
+
def last_input_file
|
24
|
+
$FILENAME
|
25
|
+
end
|
26
|
+
|
27
|
+
def last_input_line_number
|
28
|
+
$.
|
29
|
+
end
|
30
|
+
|
31
|
+
def last_input
|
32
|
+
$_
|
33
|
+
end
|
34
|
+
|
35
|
+
# program
|
36
|
+
def program_name
|
37
|
+
$0
|
38
|
+
end
|
39
|
+
|
40
|
+
def program_arguments
|
41
|
+
$:
|
42
|
+
end
|
43
|
+
|
44
|
+
def loaded_programs
|
45
|
+
$"
|
46
|
+
end
|
47
|
+
|
48
|
+
def program_data
|
49
|
+
::DATA
|
50
|
+
end
|
51
|
+
|
52
|
+
def child_program_status
|
53
|
+
$CHILD_STATUS
|
54
|
+
end
|
55
|
+
|
56
|
+
# system info
|
57
|
+
def environment
|
58
|
+
::ENV
|
59
|
+
end
|
60
|
+
alias env environment
|
61
|
+
|
62
|
+
def working_directory
|
63
|
+
Dir.pwd
|
64
|
+
end
|
65
|
+
|
66
|
+
def platform
|
67
|
+
::RUBY_PLATFORM
|
68
|
+
end
|
69
|
+
|
70
|
+
def process_id
|
71
|
+
$$
|
72
|
+
end
|
73
|
+
|
74
|
+
def load_path
|
75
|
+
$:
|
76
|
+
end
|
77
|
+
|
78
|
+
# current
|
79
|
+
def current_line
|
80
|
+
__LINE__
|
81
|
+
end
|
82
|
+
|
83
|
+
def current_method # 1.9
|
84
|
+
__method__
|
85
|
+
end
|
86
|
+
|
87
|
+
def current_callstack
|
88
|
+
caller
|
89
|
+
end
|
90
|
+
|
91
|
+
# dealing with strings
|
92
|
+
def gets_separator
|
93
|
+
$/
|
94
|
+
end
|
95
|
+
|
96
|
+
def join_separator
|
97
|
+
$,
|
98
|
+
end
|
99
|
+
|
100
|
+
def print_separator
|
101
|
+
$,
|
102
|
+
end
|
103
|
+
|
104
|
+
def split_separator
|
105
|
+
$;
|
106
|
+
end
|
107
|
+
|
108
|
+
# misc
|
109
|
+
def security_level
|
110
|
+
$SAFE
|
111
|
+
end
|
112
|
+
|
113
|
+
def warnings_activated?
|
114
|
+
$VERBOSE
|
115
|
+
end
|
116
|
+
|
117
|
+
def debug_activated?
|
118
|
+
$DEBUG
|
119
|
+
end
|
120
|
+
|
121
|
+
def last_exception
|
122
|
+
$!
|
123
|
+
end
|
124
|
+
|
125
|
+
# defined objects
|
126
|
+
def global_variables
|
127
|
+
global_variables
|
128
|
+
end
|
129
|
+
|
130
|
+
def global_constants
|
131
|
+
Object.constants
|
132
|
+
end
|
133
|
+
|
134
|
+
# encoding (1.9)
|
135
|
+
def source_encoding
|
136
|
+
__ENCODING__
|
137
|
+
end
|
138
|
+
|
139
|
+
def external_encoding
|
140
|
+
Encoding.default_external
|
141
|
+
end
|
142
|
+
|
143
|
+
def internal_encoding
|
144
|
+
Encoding.default_internal
|
145
|
+
end
|
146
|
+
|
147
|
+
# ruby version info
|
148
|
+
def ruby_version
|
149
|
+
::RUBY_VERSION
|
150
|
+
end
|
151
|
+
|
152
|
+
def ruby_patchlevel
|
153
|
+
::RUBY_PATCHLEVEL
|
154
|
+
end
|
155
|
+
|
156
|
+
def ruby_description
|
157
|
+
::RUBY_DESCRIPTION
|
158
|
+
end
|
159
|
+
|
160
|
+
def ruby_release_date
|
161
|
+
::RUBY_RELEASE_DATE
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
@@ -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
|
+
|
@@ -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,18 @@
|
|
1
|
+
def activate_warnings!
|
2
|
+
$VERBOSE = true
|
3
|
+
end
|
4
|
+
|
5
|
+
def deactivate_warnings!
|
6
|
+
$VERBOSE = false
|
7
|
+
end
|
8
|
+
|
9
|
+
def library?
|
10
|
+
__FILE__ != $PROGRAM_NAME
|
11
|
+
end
|
12
|
+
|
13
|
+
def ignore_sigint! # ctrl+c
|
14
|
+
Signal.trap *%w|SIGINT IGNORE|
|
15
|
+
end
|
16
|
+
|
17
|
+
# J-_-L
|
18
|
+
|
data/lib/zucker/2/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,19 @@
|
|
1
|
+
def sandbox(rescueblock_or_default=nil)
|
2
|
+
Thread.start do
|
3
|
+
$SAFE = 4
|
4
|
+
yield
|
5
|
+
end.value
|
6
|
+
rescue SecurityError => e
|
7
|
+
if !rescueblock_or_default.nil?
|
8
|
+
if rescueblock_or_default.is_a? Proc
|
9
|
+
rescueblock_or_default.call e
|
10
|
+
else
|
11
|
+
rescueblock_or_default
|
12
|
+
end
|
13
|
+
else
|
14
|
+
raise e
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# J-_-L
|
19
|
+
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'zucker/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
|
+
|
14
|
+
a.each{ |e|
|
15
|
+
(e.D{|value| "This is a: #{value}"}).should == e
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should puts .inspect if no block is given (and not change the object's value)" do
|
20
|
+
capture_stdout do
|
21
|
+
a[0].D
|
22
|
+
a[1].D
|
23
|
+
a[6].D
|
24
|
+
end.should == %{1\n"1"\ntrue\n}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should puts the block if it is given (and not change the object's value)" do
|
28
|
+
capture_stdout do
|
29
|
+
a[0].D{|value| "This is a: #{value}"}
|
30
|
+
a[1].D{|value| "This is a: #{value}"}
|
31
|
+
a[6].D{|value| "This is a: #{value}"}
|
32
|
+
end.should == %{This is a: 1\nThis is a: 1\nThis is a: true\n}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'zucker/array2proc'
|
2
|
+
|
3
|
+
describe 'Array#to_proc' do
|
4
|
+
it 'should call the method of the first symbol, using the remaining elements as paramaters' do
|
5
|
+
[1,2,3,4].map( &[:to_s, 2] ).should == ["1", "10", "11", "100"]
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should convert each element to a proc and chain it, if the first parameter is an array" do
|
9
|
+
[1,2,3,4].map( &[[:*,2],[:+,4]] ).should == [1,2,3,4].map{|i| i*2 + 4 }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'zucker/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
|
+
|