zucker 1 → 2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/CHANGELOG +14 -0
  2. data/Rakefile +14 -4
  3. data/lib/zucker.rb +0 -4
  4. data/lib/zucker/2/D.rb +19 -0
  5. data/lib/zucker/2/aliases.rb +28 -0
  6. data/lib/zucker/2/all.rb +6 -0
  7. data/lib/zucker/2/array.rb +17 -0
  8. data/lib/zucker/2/array2proc.rb +16 -0
  9. data/lib/zucker/2/binding.rb +25 -0
  10. data/lib/zucker/2/blank.rb +26 -0
  11. data/lib/zucker/2/class2proc.rb +10 -0
  12. data/lib/zucker/2/debug.rb +4 -0
  13. data/lib/zucker/2/default.rb +25 -0
  14. data/lib/zucker/2/desc/D.yaml +23 -0
  15. data/lib/zucker/2/desc/aliases.yaml +16 -0
  16. data/lib/zucker/2/desc/array.yaml +14 -0
  17. data/lib/zucker/2/desc/array2proc.yaml +17 -0
  18. data/lib/zucker/2/desc/binding.yaml +10 -0
  19. data/lib/zucker/2/desc/blank.yaml +10 -0
  20. data/lib/zucker/2/desc/class2proc.yaml +12 -0
  21. data/lib/zucker/2/desc/egonil.yaml +17 -0
  22. data/lib/zucker/2/desc/enumerable.yaml +11 -0
  23. data/lib/zucker/2/desc/hash.yaml +14 -0
  24. data/lib/zucker/2/desc/hash2proc.yaml +11 -0
  25. data/lib/zucker/2/desc/info.yaml +12 -0
  26. data/lib/zucker/2/desc/iterate.yaml +17 -0
  27. data/lib/zucker/2/desc/ivars.yaml +20 -0
  28. data/lib/zucker/2/desc/kernel.yaml +13 -0
  29. data/lib/zucker/2/desc/mcopy.yaml +12 -0
  30. data/lib/zucker/2/desc/mm.yaml +12 -0
  31. data/lib/zucker/2/desc/regexp2proc.yaml +13 -0
  32. data/lib/zucker/2/desc/sandbox.yaml +19 -0
  33. data/lib/zucker/2/desc/square_brackets_for.yaml +23 -0
  34. data/lib/zucker/2/desc/string.yaml +13 -0
  35. data/lib/zucker/2/desc/unary.yaml +15 -0
  36. data/lib/zucker/2/desc/union.yaml +11 -0
  37. data/lib/zucker/2/doc/zucker_doc.html +1494 -0
  38. data/lib/zucker/2/doc/zucker_doc.rb +498 -0
  39. data/lib/zucker/2/egonil.rb +14 -0
  40. data/lib/zucker/2/enumerable.rb +12 -0
  41. data/lib/zucker/2/hash.rb +19 -0
  42. data/lib/zucker/2/hash2proc.rb +14 -0
  43. data/lib/zucker/2/info.rb +164 -0
  44. data/lib/zucker/2/iterate.rb +23 -0
  45. data/lib/zucker/2/ivars.rb +26 -0
  46. data/lib/zucker/2/kernel.rb +18 -0
  47. data/lib/zucker/2/mcopy.rb +9 -0
  48. data/lib/zucker/2/mm.rb +31 -0
  49. data/lib/zucker/2/regexp2proc.rb +10 -0
  50. data/lib/zucker/2/sandbox.rb +19 -0
  51. data/lib/zucker/2/spec/D_spec.rb +35 -0
  52. data/lib/zucker/2/spec/aliases_spec.rb +13 -0
  53. data/lib/zucker/2/spec/array2proc_spec.rb +12 -0
  54. data/lib/zucker/2/spec/array_spec.rb +23 -0
  55. data/lib/zucker/2/spec/binding_spec.rb +8 -0
  56. data/lib/zucker/2/spec/blank_spec.rb +20 -0
  57. data/lib/zucker/2/spec/class2proc_spec.rb +9 -0
  58. data/lib/zucker/2/spec/egonil_spec.rb +30 -0
  59. data/lib/zucker/2/spec/enumerable_spec.rb +8 -0
  60. data/lib/zucker/2/spec/hash2proc_spec.rb +12 -0
  61. data/lib/zucker/2/spec/hash_spec.rb +17 -0
  62. data/lib/zucker/2/spec/info_spec.rb +8 -0
  63. data/lib/zucker/2/spec/iterate_spec.rb +54 -0
  64. data/lib/zucker/2/spec/ivars_spec.rb +20 -0
  65. data/lib/zucker/2/spec/kernel_spec.rb +28 -0
  66. data/lib/zucker/2/spec/mcopy_spec.rb +13 -0
  67. data/lib/zucker/2/spec/mm_spec.rb +8 -0
  68. data/lib/zucker/2/spec/regexp2proc_spec.rb +9 -0
  69. data/lib/zucker/2/spec/sandbox_spec.rb +26 -0
  70. data/lib/zucker/2/spec/spec_helper.rb +18 -0
  71. data/lib/zucker/2/spec/square_brackets_for_spec.rb +59 -0
  72. data/lib/zucker/2/spec/string_spec.rb +29 -0
  73. data/lib/zucker/2/spec/unary_spec.rb +26 -0
  74. data/lib/zucker/2/spec/union_spec.rb +15 -0
  75. data/lib/zucker/2/square_brackets_for.rb +20 -0
  76. data/lib/zucker/2/string.rb +25 -0
  77. data/lib/zucker/2/unary.rb +22 -0
  78. data/lib/zucker/2/union.rb +14 -0
  79. data/lib/zucker/all.rb +2 -2
  80. data/lib/zucker/array2proc.rb +7 -1
  81. data/lib/zucker/debug.rb +3 -3
  82. data/lib/zucker/default.rb +1 -1
  83. data/lib/zucker/desc/D.yaml +1 -1
  84. data/lib/zucker/desc/array.yaml +1 -1
  85. data/lib/zucker/desc/array2proc.yaml +5 -1
  86. data/lib/zucker/desc/binding.yaml +2 -2
  87. data/lib/zucker/desc/blank.yaml +1 -3
  88. data/lib/zucker/desc/egonil.yaml +1 -1
  89. data/lib/zucker/desc/enumerable.yaml +1 -1
  90. data/lib/zucker/desc/hash.yaml +4 -2
  91. data/lib/zucker/desc/info.yaml +12 -0
  92. data/lib/zucker/desc/kernel.yaml +2 -2
  93. data/lib/zucker/desc/mm.yaml +3 -3
  94. data/lib/zucker/desc/regexp2proc.yaml +1 -1
  95. data/lib/zucker/desc/sandbox.yaml +6 -1
  96. data/lib/zucker/desc/string.yaml +1 -1
  97. data/lib/zucker/desc/unary.yaml +2 -2
  98. data/lib/zucker/desc/union.yaml +1 -1
  99. data/lib/zucker/doc/zucker_doc.html +441 -233
  100. data/lib/zucker/doc/zucker_doc.rb +32 -27
  101. data/lib/zucker/hash.rb +3 -3
  102. data/lib/zucker/info.rb +164 -0
  103. data/lib/zucker/ivars.rb +1 -1
  104. data/lib/zucker/kernel.rb +2 -4
  105. data/lib/zucker/sandbox.rb +7 -3
  106. data/lib/zucker/spec/D_spec.rb +15 -7
  107. data/lib/zucker/spec/aliases_spec.rb +3 -24
  108. data/lib/zucker/spec/array2proc_spec.rb +5 -1
  109. data/lib/zucker/spec/array_spec.rb +1 -1
  110. data/lib/zucker/spec/binding_spec.rb +4 -2
  111. data/lib/zucker/spec/blank_spec.rb +1 -1
  112. data/lib/zucker/spec/class2proc_spec.rb +1 -1
  113. data/lib/zucker/spec/egonil_spec.rb +1 -1
  114. data/lib/zucker/spec/enumerable_spec.rb +1 -1
  115. data/lib/zucker/spec/hash2proc_spec.rb +1 -1
  116. data/lib/zucker/spec/hash_spec.rb +10 -3
  117. data/lib/zucker/spec/info_spec.rb +8 -0
  118. data/lib/zucker/spec/iterate_spec.rb +1 -1
  119. data/lib/zucker/spec/ivars_spec.rb +1 -1
  120. data/lib/zucker/spec/kernel_spec.rb +1 -7
  121. data/lib/zucker/spec/mcopy_spec.rb +1 -1
  122. data/lib/zucker/spec/mm_spec.rb +4 -2
  123. data/lib/zucker/spec/regexp2proc_spec.rb +1 -1
  124. data/lib/zucker/spec/sandbox_spec.rb +22 -3
  125. data/lib/zucker/spec/spec_helper.rb +18 -0
  126. data/lib/zucker/spec/square_brackets_for_spec.rb +1 -1
  127. data/lib/zucker/spec/string_spec.rb +7 -7
  128. data/lib/zucker/spec/unary_spec.rb +1 -1
  129. data/lib/zucker/spec/union_spec.rb +8 -6
  130. metadata +207 -131
  131. data/lib/zucker/doc/meta.yaml +0 -4
@@ -0,0 +1,14 @@
1
+ def egonil(nil_value = nil)
2
+ yield
3
+ rescue NoMethodError => e
4
+ if e.message =~ /NilClass$/
5
+ nil_value
6
+ else
7
+ raise NoMethodError
8
+ end
9
+ end
10
+
11
+ alias nn egonil
12
+
13
+ # J-_-L
14
+
@@ -0,0 +1,12 @@
1
+ module Enumerable
2
+ def mash
3
+ ret = {}
4
+ each{ |kv|
5
+ ret.store *( yield(kv)[0,2] )
6
+ }
7
+ ret
8
+ end
9
+ end
10
+
11
+ # J-_-L
12
+
@@ -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,14 @@
1
+ class Hash
2
+ def to_proc
3
+ Proc.new{ |obj|
4
+ if self.member? obj
5
+ self[obj].to_proc.call obj
6
+ else
7
+ obj
8
+ end
9
+ }
10
+ end
11
+ end
12
+
13
+ # J-_-L
14
+
@@ -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
+
@@ -0,0 +1,9 @@
1
+ class Object
2
+ def mcopy
3
+ Marshal.load Marshal.dump self
4
+ end
5
+ # alias copy mcopy
6
+ end
7
+
8
+ # J-_-L
9
+
@@ -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,10 @@
1
+ class Regexp
2
+ def to_proc
3
+ proc do |e|
4
+ e.to_s[self]
5
+ end
6
+ end
7
+ end
8
+
9
+ # J-_-L
10
+
@@ -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,13 @@
1
+ require 'zucker/aliases'
2
+
3
+ describe '(aliases)' do
4
+ it 'should create these aliases' do
5
+ # see aliases.rb for aliase list
6
+ end
7
+
8
+ it 'should define these constants' do
9
+ Infinity.finite?.should == false
10
+ NaN.nan?.should == true
11
+ end
12
+ end
13
+
@@ -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
+