zucker 5 → 6
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +8 -0
- data/Rakefile +4 -3
- data/lib/zucker.rb +13 -4
- data/lib/zucker/6/alias_for.rb +21 -0
- data/lib/zucker/6/aliases.rb +51 -0
- data/lib/zucker/6/all.rb +4 -0
- data/lib/zucker/6/array.rb +25 -0
- data/lib/zucker/6/array2proc.rb +18 -0
- data/lib/zucker/6/binding.rb +34 -0
- data/lib/zucker/6/blank.rb +28 -0
- data/lib/zucker/6/cc.rb +25 -0
- data/lib/zucker/6/class2proc.rb +12 -0
- data/lib/zucker/6/control.rb +4 -0
- data/lib/zucker/6/dd.rb +23 -0
- data/lib/zucker/6/debug.rb +4 -0
- data/lib/zucker/6/default.rb +4 -0
- data/lib/zucker/6/egonil.rb +27 -0
- data/lib/zucker/6/engine.rb +68 -0
- data/lib/zucker/6/enumerable.rb +14 -0
- data/lib/zucker/6/env.rb +4 -0
- data/lib/zucker/6/extensions.rb +4 -0
- data/lib/zucker/6/hash.rb +27 -0
- data/lib/zucker/6/hash2proc.rb +16 -0
- data/lib/zucker/6/info.rb +192 -0
- data/lib/zucker/6/iterate.rb +27 -0
- data/lib/zucker/6/ivars.rb +28 -0
- data/lib/zucker/6/kernel.rb +34 -0
- data/lib/zucker/6/mcopy.rb +10 -0
- data/lib/zucker/6/mm.rb +34 -0
- data/lib/zucker/6/not.rb +19 -0
- data/lib/zucker/6/object.rb +4 -0
- data/lib/zucker/6/oo.rb +17 -0
- data/lib/zucker/6/os.rb +54 -0
- data/lib/zucker/6/qq.rb +12 -0
- data/lib/zucker/6/regexp2proc.rb +12 -0
- data/lib/zucker/6/sandbox.rb +25 -0
- data/lib/zucker/6/shortcuts.rb +4 -0
- data/lib/zucker/6/square_brackets_for.rb +21 -0
- data/lib/zucker/6/string.rb +54 -0
- data/lib/zucker/6/tap.rb +11 -0
- data/lib/zucker/6/to_proc.rb +4 -0
- data/lib/zucker/6/unary.rb +24 -0
- data/lib/zucker/6/union.rb +16 -0
- data/lib/zucker/6/version.rb +118 -0
- data/lib/zucker/binding.rb +3 -3
- data/lib/zucker/egonil.rb +17 -6
- data/lib/zucker/engine.rb +1 -2
- data/lib/zucker/info.rb +16 -1
- data/lib/zucker/iterate.rb +4 -3
- data/lib/zucker/oo.rb +1 -1
- data/lib/zucker/os.rb +11 -6
- metadata +227 -174
data/lib/zucker/6/qq.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
private
|
5
|
+
|
6
|
+
def sandbox(rescueblock_or_default=nil)
|
7
|
+
Thread.start do
|
8
|
+
$SAFE = 4
|
9
|
+
yield
|
10
|
+
end.value
|
11
|
+
rescue SecurityError => e
|
12
|
+
if !rescueblock_or_default.nil?
|
13
|
+
if rescueblock_or_default.is_a? Proc
|
14
|
+
rescueblock_or_default.call e
|
15
|
+
else
|
16
|
+
rescueblock_or_default
|
17
|
+
end
|
18
|
+
else
|
19
|
+
raise e
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# J-_-L
|
25
|
+
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class Module
|
4
|
+
def square_brackets_for(ivar, assignment = true)
|
5
|
+
# undef [] if respond_to? :[]
|
6
|
+
# undef []= if respond_to? :[]=
|
7
|
+
|
8
|
+
define_method :[] do |key|
|
9
|
+
(instance_variable_get :"@#{ivar}")[key]
|
10
|
+
end
|
11
|
+
|
12
|
+
if assignment
|
13
|
+
define_method :[]= do |key, value|
|
14
|
+
(instance_variable_get :"@#{ivar}")[key] = value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# J-_-L
|
21
|
+
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class String
|
4
|
+
def -(rem)
|
5
|
+
gsub( Regexp === rem ? rem : rem.to_s, '' )
|
6
|
+
end
|
7
|
+
|
8
|
+
def ^(pos)
|
9
|
+
pos = pos.to_i
|
10
|
+
if pos >= 0
|
11
|
+
self[pos..-1]
|
12
|
+
else
|
13
|
+
self[0...pos]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def lchomp(arg = $/)
|
18
|
+
reverse.chomp(arg).reverse
|
19
|
+
end
|
20
|
+
|
21
|
+
def lchomp!(arg = $/)
|
22
|
+
reverse.chomp!(arg).reverse
|
23
|
+
end
|
24
|
+
|
25
|
+
def ords
|
26
|
+
unpack 'C*'
|
27
|
+
# bytes.to_a
|
28
|
+
end
|
29
|
+
|
30
|
+
def constantize(default_value = nil) # always uses global scope as in AS... is this good?
|
31
|
+
get_constant = lambda{
|
32
|
+
self.split(/::/).inject( Object ){ |base_constant, current_constant|
|
33
|
+
base_constant.const_get current_constant
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
if !default_value && !block_given?
|
38
|
+
get_constant.call
|
39
|
+
else
|
40
|
+
begin
|
41
|
+
get_constant.call
|
42
|
+
rescue NameError
|
43
|
+
if block_given?
|
44
|
+
yield self
|
45
|
+
else
|
46
|
+
default_value
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
# J-_-L
|
54
|
+
|
data/lib/zucker/6/tap.rb
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
require 'date'
|
3
|
+
require 'time'
|
4
|
+
|
5
|
+
module RubyVersion
|
6
|
+
class << self
|
7
|
+
def to_s
|
8
|
+
RUBY_VERSION
|
9
|
+
end
|
10
|
+
|
11
|
+
# comparable
|
12
|
+
def <=>(other)
|
13
|
+
value = case other
|
14
|
+
when Integer
|
15
|
+
RUBY_VERSION.to_i
|
16
|
+
when Float
|
17
|
+
RUBY_VERSION.to_f
|
18
|
+
when String
|
19
|
+
RUBY_VERSION
|
20
|
+
when Date, Time
|
21
|
+
other.class.parse(RUBY_RELEASE_DATE)
|
22
|
+
else
|
23
|
+
other = other.to_s
|
24
|
+
RUBY_VERSION
|
25
|
+
end
|
26
|
+
value <=> other
|
27
|
+
end
|
28
|
+
include Comparable
|
29
|
+
|
30
|
+
# chaining for dsl-like language
|
31
|
+
def is?(other = nil)
|
32
|
+
if other
|
33
|
+
RubyVersion == other
|
34
|
+
else
|
35
|
+
RubyVersion
|
36
|
+
end
|
37
|
+
end
|
38
|
+
alias is is?
|
39
|
+
|
40
|
+
# aliases
|
41
|
+
alias below <
|
42
|
+
alias below? <
|
43
|
+
alias at_most <=
|
44
|
+
alias at_most? <=
|
45
|
+
alias above >
|
46
|
+
alias above? >
|
47
|
+
alias at_least >=
|
48
|
+
alias at_least? >=
|
49
|
+
alias exactly ==
|
50
|
+
alias exactly? ==
|
51
|
+
def not(other)
|
52
|
+
self != other
|
53
|
+
end
|
54
|
+
alias not? not
|
55
|
+
alias between between?
|
56
|
+
|
57
|
+
# compare dates
|
58
|
+
def newer_than(other)
|
59
|
+
if other.is_a? Date or other.is_a? Time
|
60
|
+
RubyVersion > other
|
61
|
+
else
|
62
|
+
RUBY_RELEASE_DATE > other.to_s
|
63
|
+
end
|
64
|
+
end
|
65
|
+
alias newer_than? newer_than
|
66
|
+
|
67
|
+
def older_than(other)
|
68
|
+
if other.is_a? Date or other.is_a? Time
|
69
|
+
RubyVersion < other
|
70
|
+
else
|
71
|
+
RUBY_RELEASE_DATE < other.to_s
|
72
|
+
end
|
73
|
+
end
|
74
|
+
alias older_than? older_than
|
75
|
+
|
76
|
+
def released_today
|
77
|
+
RubyVersion.date == Date.today
|
78
|
+
end
|
79
|
+
alias released_today? released_today
|
80
|
+
|
81
|
+
# accessors
|
82
|
+
|
83
|
+
def major
|
84
|
+
RUBY_VERSION.to_i
|
85
|
+
end
|
86
|
+
alias main major
|
87
|
+
|
88
|
+
def minor
|
89
|
+
RUBY_VERSION.split('.')[1].to_i
|
90
|
+
end
|
91
|
+
alias mini minor
|
92
|
+
|
93
|
+
def tiny
|
94
|
+
RUBY_VERSION.split('.')[2].to_i
|
95
|
+
end
|
96
|
+
|
97
|
+
alias teeny tiny
|
98
|
+
|
99
|
+
def patchlevel
|
100
|
+
RUBY_PATCHLEVEL
|
101
|
+
end
|
102
|
+
|
103
|
+
def platform
|
104
|
+
RUBY_PLATFORM
|
105
|
+
end
|
106
|
+
|
107
|
+
def release_date
|
108
|
+
Date.parse RUBY_RELEASE_DATE
|
109
|
+
end
|
110
|
+
alias date release_date
|
111
|
+
|
112
|
+
def description
|
113
|
+
RUBY_DESCRIPTION
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
# J-_-L
|
data/lib/zucker/binding.rb
CHANGED
data/lib/zucker/egonil.rb
CHANGED
@@ -1,12 +1,23 @@
|
|
1
1
|
require 'zucker'
|
2
2
|
|
3
|
-
|
3
|
+
# code by Yohan, slightly edited and comments by me
|
4
|
+
def egonil(&block)
|
5
|
+
# grip methods
|
6
|
+
ori_method_missing = NilClass.instance_method(:method_missing)
|
7
|
+
catch_method_missing = NilClass.instance_method(:catch_method_missing)
|
8
|
+
# activate ego mode
|
9
|
+
NilClass.send :define_method, :method_missing, catch_method_missing
|
10
|
+
# run code
|
4
11
|
yield
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
12
|
+
ensure
|
13
|
+
# no matter what happens: restore default nil behaviour
|
14
|
+
NilClass.send :define_method, :method_missing, ori_method_missing
|
15
|
+
end
|
16
|
+
|
17
|
+
# this is the ego nil
|
18
|
+
class NilClass
|
19
|
+
def catch_method_missing(m, *args, &block)
|
20
|
+
nil
|
10
21
|
end
|
11
22
|
end
|
12
23
|
|
data/lib/zucker/engine.rb
CHANGED
@@ -5,8 +5,7 @@ module RubyEngine
|
|
5
5
|
@interpreter = case
|
6
6
|
when RUBY_PLATFORM == 'parrot'
|
7
7
|
'cardinal'
|
8
|
-
when Object.
|
9
|
-
Object.constants.include?( 'RUBY_ENGINE' )
|
8
|
+
when Object.const_defined?(:RUBY_ENGINE)
|
10
9
|
if RUBY_ENGINE == 'ruby'
|
11
10
|
if RUBY_DESCRIPTION =~ /Enterprise/
|
12
11
|
'ree'
|
data/lib/zucker/info.rb
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
require 'zucker'
|
2
|
+
require 'rbconfig'
|
3
|
+
require 'etc'
|
2
4
|
|
3
5
|
module Info
|
4
6
|
class << self
|
@@ -75,6 +77,15 @@ module Info
|
|
75
77
|
$:
|
76
78
|
end
|
77
79
|
|
80
|
+
# user
|
81
|
+
def user_login
|
82
|
+
Etc.getlogin
|
83
|
+
end
|
84
|
+
|
85
|
+
def user_name
|
86
|
+
Etc.getpwnam(user_login).gecos.split(',')[0]
|
87
|
+
end
|
88
|
+
|
78
89
|
# current
|
79
90
|
|
80
91
|
def current_file # __FILE__
|
@@ -158,7 +169,7 @@ module Info
|
|
158
169
|
end
|
159
170
|
|
160
171
|
# ruby version info
|
161
|
-
def ruby_version
|
172
|
+
def ruby_version # also see the RubyVersion cube
|
162
173
|
::RUBY_VERSION
|
163
174
|
end
|
164
175
|
|
@@ -173,5 +184,9 @@ module Info
|
|
173
184
|
def ruby_release_date
|
174
185
|
::RUBY_RELEASE_DATE
|
175
186
|
end
|
187
|
+
|
188
|
+
def ruby_engine # warning! not support by every implementation. It's saver to use the RubyEngine cube
|
189
|
+
::RUBY_ENGINE
|
190
|
+
end
|
176
191
|
end
|
177
192
|
|
data/lib/zucker/iterate.rb
CHANGED
@@ -9,10 +9,11 @@ def iterate(*params)
|
|
9
9
|
if block_given?
|
10
10
|
first.map{|e| yield e }
|
11
11
|
else
|
12
|
-
first.map
|
12
|
+
first.map.to_enum
|
13
13
|
end
|
14
|
-
else
|
15
|
-
|
14
|
+
else # multiple params
|
15
|
+
max_size = [first, *params].max_by(&:count).size
|
16
|
+
padded_first = first.to_a + [nil]*(max_size - first.count) # append nils
|
16
17
|
obj = padded_first.zip *params
|
17
18
|
if block_given?
|
18
19
|
obj.map{|es| yield *es }
|
data/lib/zucker/oo.rb
CHANGED
data/lib/zucker/os.rb
CHANGED
@@ -16,27 +16,32 @@ module OS
|
|
16
16
|
module_function
|
17
17
|
|
18
18
|
def linux?
|
19
|
-
OS.is? /linux|cygwin/
|
19
|
+
OS.is?( /linux|cygwin/ )
|
20
20
|
end
|
21
21
|
|
22
22
|
def mac?
|
23
|
-
OS.is? /mac|darwin/
|
23
|
+
OS.is?( /mac|darwin/ )
|
24
24
|
end
|
25
25
|
|
26
26
|
def bsd?
|
27
|
-
OS.is? /bsd/
|
27
|
+
OS.is?( /bsd/ )
|
28
28
|
end
|
29
29
|
|
30
30
|
def windows?
|
31
|
-
OS.is? /mswin|win|mingw/
|
31
|
+
OS.is?( /mswin|win|mingw/ )
|
32
32
|
end
|
33
33
|
|
34
34
|
def solaris?
|
35
|
-
OS.is? /solaris|sunos/
|
35
|
+
OS.is?( /solaris|sunos/ )
|
36
36
|
end
|
37
37
|
|
38
38
|
def posix?
|
39
|
-
linux? or mac? or bsd? or solaris? or
|
39
|
+
linux? or mac? or bsd? or solaris? or begin
|
40
|
+
fork do end
|
41
|
+
true
|
42
|
+
rescue NotImplementedError, NoMethodError
|
43
|
+
false
|
44
|
+
end
|
40
45
|
end
|
41
46
|
|
42
47
|
#def symbian?
|