zucker 6 → 7
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +5 -0
- data/Rakefile +1 -2
- data/lib/zucker.rb +1 -1
- data/lib/zucker/7/alias_for.rb +21 -0
- data/lib/zucker/7/aliases.rb +51 -0
- data/lib/zucker/7/all.rb +4 -0
- data/lib/zucker/7/array.rb +25 -0
- data/lib/zucker/7/array2proc.rb +18 -0
- data/lib/zucker/7/binding.rb +34 -0
- data/lib/zucker/7/blank.rb +28 -0
- data/lib/zucker/7/cc.rb +25 -0
- data/lib/zucker/7/class2proc.rb +12 -0
- data/lib/zucker/7/control.rb +4 -0
- data/lib/zucker/7/dd.rb +23 -0
- data/lib/zucker/7/debug.rb +4 -0
- data/lib/zucker/7/default.rb +4 -0
- data/lib/zucker/7/egonil.rb +27 -0
- data/lib/zucker/7/engine.rb +68 -0
- data/lib/zucker/7/enumerable.rb +14 -0
- data/lib/zucker/7/env.rb +4 -0
- data/lib/zucker/7/extensions.rb +4 -0
- data/lib/zucker/7/hash.rb +27 -0
- data/lib/zucker/7/hash2proc.rb +16 -0
- data/lib/zucker/7/info.rb +192 -0
- data/lib/zucker/7/iterate.rb +27 -0
- data/lib/zucker/7/ivars.rb +28 -0
- data/lib/zucker/7/kernel.rb +34 -0
- data/lib/zucker/7/mcopy.rb +10 -0
- data/lib/zucker/7/mm.rb +34 -0
- data/lib/zucker/7/not.rb +19 -0
- data/lib/zucker/7/object.rb +4 -0
- data/lib/zucker/7/oo.rb +17 -0
- data/lib/zucker/7/os.rb +54 -0
- data/lib/zucker/7/qq.rb +12 -0
- data/lib/zucker/7/regexp2proc.rb +12 -0
- data/lib/zucker/7/sandbox.rb +25 -0
- data/lib/zucker/7/shortcuts.rb +4 -0
- data/lib/zucker/7/square_brackets_for.rb +21 -0
- data/lib/zucker/7/string.rb +54 -0
- data/lib/zucker/7/tap.rb +11 -0
- data/lib/zucker/7/to_proc.rb +4 -0
- data/lib/zucker/7/unary.rb +24 -0
- data/lib/zucker/7/union.rb +16 -0
- data/lib/zucker/7/version.rb +118 -0
- data/lib/zucker/os.rb +1 -1
- metadata +43 -2
data/CHANGELOG
CHANGED
@@ -1,3 +1,7 @@
|
|
1
|
+
2010-10-03 | Zucker 7
|
2
|
+
* fixed critical OS.windows? bug
|
3
|
+
|
4
|
+
|
1
5
|
2010-10-03 | Zucker 6
|
2
6
|
* no new cubes
|
3
7
|
* bugfix for OS.posix?
|
@@ -6,6 +10,7 @@
|
|
6
10
|
* changed egonil semantics ( using method_missing, see http://rbjl.net/26/catch_nil.rb )
|
7
11
|
* bugfix for vv
|
8
12
|
|
13
|
+
|
9
14
|
2010-09-04 | Zucker 5
|
10
15
|
* debug edition - added two debug helpers: oo (output line, method, file) and cc (output method callstack)
|
11
16
|
* renamed cube D to dd add added more debug aliases (for mm and binding)
|
data/Rakefile
CHANGED
@@ -83,6 +83,5 @@ task 'release' => %w[gem] do
|
|
83
83
|
`git push origin master --tags`
|
84
84
|
last_gem = Dir['pkg/zucker-*.gem'].sort[-1] # TODO better sorting or date for zucker-10
|
85
85
|
exit if last_gem =~ /next/ # some error happened, do not pollute rubygems.org
|
86
|
-
puts last_gem
|
87
|
-
#`gem push pkg/#{last_gem}`
|
86
|
+
puts "gem push pkg/#{last_gem}"
|
88
87
|
end
|
data/lib/zucker.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
def alias_for(m, *aliases)
|
4
|
+
aliases.each{ |a|
|
5
|
+
class_eval "alias #{a} #{m}"
|
6
|
+
}
|
7
|
+
end
|
8
|
+
alias aliases_for alias_for
|
9
|
+
|
10
|
+
class Module
|
11
|
+
def alias_method_for(m, *alias_methods)
|
12
|
+
alias_methods.each{ |a|
|
13
|
+
class_eval do
|
14
|
+
alias_method a.to_sym, m.to_sym
|
15
|
+
end
|
16
|
+
}
|
17
|
+
end
|
18
|
+
alias alias_methods_for alias_method_for
|
19
|
+
end
|
20
|
+
|
21
|
+
# J-_-L
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
alias is_an? is_a? # thanks to utility_belt
|
5
|
+
end
|
6
|
+
|
7
|
+
module Enumerable
|
8
|
+
alias with zip
|
9
|
+
alias % zip
|
10
|
+
end
|
11
|
+
|
12
|
+
class Array
|
13
|
+
alias ** product
|
14
|
+
alias contains? include?
|
15
|
+
end
|
16
|
+
|
17
|
+
class String
|
18
|
+
alias contains? include?
|
19
|
+
end
|
20
|
+
|
21
|
+
class Hash
|
22
|
+
alias + merge
|
23
|
+
end
|
24
|
+
|
25
|
+
class Binding
|
26
|
+
#alias [] eval
|
27
|
+
def [](expr)
|
28
|
+
self.eval "#{expr}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
class << File
|
33
|
+
alias filename basename # thanks rdp :)
|
34
|
+
end
|
35
|
+
|
36
|
+
class << Dir
|
37
|
+
def join(*args)
|
38
|
+
File.join(*args)
|
39
|
+
end
|
40
|
+
|
41
|
+
def split(*args)
|
42
|
+
File.split(*args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
# constants - who would use these in real-world code for other things?
|
47
|
+
Infinity = 1.0 / 0.0 # or 2*Float::MAX or Float::INFINITY
|
48
|
+
NaN = 0.0 / 0.0
|
49
|
+
|
50
|
+
# J-_-L
|
51
|
+
|
data/lib/zucker/7/all.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class Array
|
4
|
+
def ^(other) # TODO: more efficient
|
5
|
+
(self - other) +
|
6
|
+
(other - self)
|
7
|
+
end
|
8
|
+
|
9
|
+
# can take an argument & block to be Rails compatible
|
10
|
+
def sum(identity = 0, &block)
|
11
|
+
# inject(:+)
|
12
|
+
if block_given?
|
13
|
+
map(&block).sum( identity )
|
14
|
+
else
|
15
|
+
inject(:+) || identity
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def chrs
|
20
|
+
self.pack 'C*'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# J-_-L
|
25
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class Binding
|
4
|
+
def inspect
|
5
|
+
put_vars = lambda { |array|
|
6
|
+
if array.empty?
|
7
|
+
' - none'
|
8
|
+
else
|
9
|
+
array.map{|e|
|
10
|
+
val = self.eval "#{e}"
|
11
|
+
val = val.is_a?( Binding ) ? val.to_s : val.inspect
|
12
|
+
" - #{e}: #{ val }"
|
13
|
+
}.join "\n"
|
14
|
+
end
|
15
|
+
}
|
16
|
+
|
17
|
+
"#{self.to_s}
|
18
|
+
local vars
|
19
|
+
#{ put_vars[ self.eval 'local_variables' ] }
|
20
|
+
(instance vars)
|
21
|
+
#{ put_vars[ self.eval 'instance_variables' ] }
|
22
|
+
self
|
23
|
+
- #{self.eval 'self'}
|
24
|
+
block_given?
|
25
|
+
- #{self.eval 'block_given?'}"
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
alias v binding
|
31
|
+
alias vv binding
|
32
|
+
|
33
|
+
# J-_-L
|
34
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
def blank?
|
5
|
+
if respond_to? :empty? then empty? else !self end
|
6
|
+
end
|
7
|
+
|
8
|
+
def present?
|
9
|
+
!blank?
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
{ # what to do # for which classes
|
15
|
+
lambda{ true } => [FalseClass, NilClass],
|
16
|
+
lambda{ false } => [TrueClass, Numeric],
|
17
|
+
lambda{ empty? } => [Array, Hash],
|
18
|
+
lambda{ self !~ /\S/ } => [String],
|
19
|
+
lambda{ self == // } => [Regexp],
|
20
|
+
|
21
|
+
}.each{ |action, klass_array|
|
22
|
+
klass_array.each{ |klass|
|
23
|
+
klass.send :define_method, :blank?, &action
|
24
|
+
}
|
25
|
+
}
|
26
|
+
|
27
|
+
# J-_-L
|
28
|
+
|
data/lib/zucker/7/cc.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
private
|
5
|
+
|
6
|
+
def c(show_irb = false)
|
7
|
+
method_stack = caller.reverse.map{ |m|
|
8
|
+
m.rindex( /:\d+(:in `(.*)')?$/ )
|
9
|
+
$2
|
10
|
+
}.compact
|
11
|
+
|
12
|
+
if !show_irb && a = method_stack.index( 'irb_binding' )
|
13
|
+
method_stack = [ method_stack[0], '(irb)', *method_stack[a+1..-1] ]
|
14
|
+
end
|
15
|
+
|
16
|
+
# puts method_stack.map.with_index{ |m, i|
|
17
|
+
method_stack.each_with_index{ |m, i|
|
18
|
+
puts " "*i + m
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
alias cc c
|
23
|
+
end
|
24
|
+
|
25
|
+
# J-_-L
|
data/lib/zucker/7/dd.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
def d(*args, &block)
|
5
|
+
if args.empty?
|
6
|
+
tap{
|
7
|
+
if block_given?
|
8
|
+
puts yield self
|
9
|
+
else
|
10
|
+
puts self.inspect
|
11
|
+
end
|
12
|
+
}
|
13
|
+
else
|
14
|
+
raise ArgumentError, ".d - The parser thought that the code after .d are method arguments... Please don't put a space after d or use .d() or .d{} in this case!"
|
15
|
+
# eval ...
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
alias dd d
|
20
|
+
end
|
21
|
+
|
22
|
+
# J-_-L
|
23
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
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
|
11
|
+
yield
|
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
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
alias nn egonil
|
25
|
+
|
26
|
+
# J-_-L
|
27
|
+
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
module RubyEngine
|
4
|
+
# try to guess the interpreter
|
5
|
+
@interpreter = case
|
6
|
+
when RUBY_PLATFORM == 'parrot'
|
7
|
+
'cardinal'
|
8
|
+
when Object.const_defined?(:RUBY_ENGINE)
|
9
|
+
if RUBY_ENGINE == 'ruby'
|
10
|
+
if RUBY_DESCRIPTION =~ /Enterprise/
|
11
|
+
'ree'
|
12
|
+
else
|
13
|
+
'mri'
|
14
|
+
end
|
15
|
+
else
|
16
|
+
RUBY_ENGINE.to_s # jruby, rbx, ironruby, macruby, etc.
|
17
|
+
end
|
18
|
+
else # probably 1.8
|
19
|
+
'mri'
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
def is?(what)
|
24
|
+
what === @interpreter
|
25
|
+
end
|
26
|
+
alias is is?
|
27
|
+
|
28
|
+
def to_s
|
29
|
+
@interpreter.to_s
|
30
|
+
end
|
31
|
+
|
32
|
+
# ask methods
|
33
|
+
|
34
|
+
def mri?
|
35
|
+
RubyEngine.is? 'mri'
|
36
|
+
end
|
37
|
+
alias official_ruby? mri?
|
38
|
+
alias ruby? mri?
|
39
|
+
|
40
|
+
def jruby?
|
41
|
+
RubyEngine.is? 'jruby'
|
42
|
+
end
|
43
|
+
alias java? jruby?
|
44
|
+
|
45
|
+
def rubinius?
|
46
|
+
RubyEngine.is? 'rbx'
|
47
|
+
end
|
48
|
+
alias rbx? rubinius?
|
49
|
+
|
50
|
+
def ree?
|
51
|
+
RubyEngine.is? 'ree'
|
52
|
+
end
|
53
|
+
alias enterprise? ree?
|
54
|
+
|
55
|
+
def ironruby?
|
56
|
+
RubyEngine.is? 'ironruby'
|
57
|
+
end
|
58
|
+
alias iron_ruby? ironruby?
|
59
|
+
|
60
|
+
def cardinal?
|
61
|
+
RubyEngine.is? 'cardinal'
|
62
|
+
end
|
63
|
+
alias parrot? cardinal?
|
64
|
+
alias perl? cardinal?
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
# J-_-L
|
data/lib/zucker/7/env.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class Hash
|
4
|
+
def self.zip(keys,values)
|
5
|
+
Hash[ *keys.zip( values ).flatten ]
|
6
|
+
end
|
7
|
+
|
8
|
+
def <<(other)
|
9
|
+
case
|
10
|
+
when other.is_a?(Hash)
|
11
|
+
merge! other
|
12
|
+
when other.is_a?(Enumerable) || other.respond_to?(:to_splat)
|
13
|
+
merge! Hash[*other]
|
14
|
+
else
|
15
|
+
raise TypeError, 'can only append other Hashs and Enumerables (or Classes that implement to_splat)'
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def &(other)
|
20
|
+
Hash[ *select{ |k,v|
|
21
|
+
other[k] == v
|
22
|
+
}.flatten ]
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# J-_-L
|
27
|
+
|
@@ -0,0 +1,192 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
require 'rbconfig'
|
3
|
+
require 'etc'
|
4
|
+
|
5
|
+
module Info
|
6
|
+
class << self
|
7
|
+
# hash like access
|
8
|
+
def [](what)
|
9
|
+
send what
|
10
|
+
end
|
11
|
+
|
12
|
+
# list available info methods
|
13
|
+
def list
|
14
|
+
singleton_methods - [:[], :list, '[]', 'list']
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
module_function
|
19
|
+
|
20
|
+
# input
|
21
|
+
def last_input_file
|
22
|
+
$FILENAME
|
23
|
+
end
|
24
|
+
|
25
|
+
def last_input_line_number
|
26
|
+
$.
|
27
|
+
end
|
28
|
+
|
29
|
+
def last_input
|
30
|
+
$_
|
31
|
+
end
|
32
|
+
|
33
|
+
# program
|
34
|
+
def program_name
|
35
|
+
$0
|
36
|
+
end
|
37
|
+
|
38
|
+
def program_arguments
|
39
|
+
$:
|
40
|
+
end
|
41
|
+
|
42
|
+
def loaded_programs
|
43
|
+
$"
|
44
|
+
end
|
45
|
+
|
46
|
+
def program_data
|
47
|
+
::DATA
|
48
|
+
end
|
49
|
+
|
50
|
+
def child_program_status
|
51
|
+
$CHILD_STATUS
|
52
|
+
end
|
53
|
+
|
54
|
+
# system info
|
55
|
+
def environment
|
56
|
+
::ENV
|
57
|
+
end
|
58
|
+
alias env environment
|
59
|
+
|
60
|
+
def working_directory
|
61
|
+
Dir.pwd
|
62
|
+
end
|
63
|
+
|
64
|
+
def platform
|
65
|
+
::RUBY_PLATFORM
|
66
|
+
end
|
67
|
+
|
68
|
+
def os
|
69
|
+
RbConfig::CONFIG['host_os']
|
70
|
+
end
|
71
|
+
|
72
|
+
def process_id
|
73
|
+
$$
|
74
|
+
end
|
75
|
+
|
76
|
+
def load_path
|
77
|
+
$:
|
78
|
+
end
|
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
|
+
|
89
|
+
# current
|
90
|
+
|
91
|
+
def current_file # __FILE__
|
92
|
+
return $` if caller[0].rindex(/:\d+(:in `.*')?$/)
|
93
|
+
end
|
94
|
+
|
95
|
+
def current_file_directory
|
96
|
+
if current_file[0,1] == '(' && current_file[-1,1] == ')'
|
97
|
+
current_file
|
98
|
+
else
|
99
|
+
File.dirname(current_file)
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def current_line # __LINE__
|
104
|
+
return $1.to_i if caller[0].rindex( /:(\d+)(:in `.*')?$/ )
|
105
|
+
end
|
106
|
+
|
107
|
+
def current_method # __method__ (except aliases)
|
108
|
+
return $1.to_sym if caller(1)[0].rindex( /\`([^\']+)\'/ )
|
109
|
+
end
|
110
|
+
|
111
|
+
def current_callstack
|
112
|
+
caller
|
113
|
+
end
|
114
|
+
|
115
|
+
# dealing with strings
|
116
|
+
def gets_separator
|
117
|
+
$/
|
118
|
+
end
|
119
|
+
|
120
|
+
def join_separator
|
121
|
+
$,
|
122
|
+
end
|
123
|
+
|
124
|
+
def print_separator
|
125
|
+
$,
|
126
|
+
end
|
127
|
+
|
128
|
+
def split_separator
|
129
|
+
$;
|
130
|
+
end
|
131
|
+
|
132
|
+
# misc
|
133
|
+
def security_level
|
134
|
+
$SAFE
|
135
|
+
end
|
136
|
+
|
137
|
+
def warnings_activated?
|
138
|
+
$VERBOSE
|
139
|
+
end
|
140
|
+
|
141
|
+
def debug_activated?
|
142
|
+
$DEBUG
|
143
|
+
end
|
144
|
+
|
145
|
+
def last_exception
|
146
|
+
$!
|
147
|
+
end
|
148
|
+
|
149
|
+
# defined objects
|
150
|
+
def global_variables
|
151
|
+
Object.send :global_variables
|
152
|
+
end
|
153
|
+
|
154
|
+
def global_constants
|
155
|
+
Object.constants
|
156
|
+
end
|
157
|
+
|
158
|
+
# encoding (1.9)
|
159
|
+
#def source_encoding
|
160
|
+
# __ENCODING__
|
161
|
+
#end
|
162
|
+
|
163
|
+
def external_encoding
|
164
|
+
Encoding.default_external
|
165
|
+
end
|
166
|
+
|
167
|
+
def internal_encoding
|
168
|
+
Encoding.default_internal
|
169
|
+
end
|
170
|
+
|
171
|
+
# ruby version info
|
172
|
+
def ruby_version # also see the RubyVersion cube
|
173
|
+
::RUBY_VERSION
|
174
|
+
end
|
175
|
+
|
176
|
+
def ruby_patchlevel
|
177
|
+
::RUBY_PATCHLEVEL
|
178
|
+
end
|
179
|
+
|
180
|
+
def ruby_description
|
181
|
+
::RUBY_DESCRIPTION
|
182
|
+
end
|
183
|
+
|
184
|
+
def ruby_release_date
|
185
|
+
::RUBY_RELEASE_DATE
|
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
|
191
|
+
end
|
192
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
def iterate(*params)
|
4
|
+
# params.shift.zip(*params).each{ |*elements| yield *elements }
|
5
|
+
raise ArgumentError, "wrong number of arguments (0)" if params.empty?
|
6
|
+
|
7
|
+
first = params.shift
|
8
|
+
if params.empty? # single param - like each
|
9
|
+
if block_given?
|
10
|
+
first.map{|e| yield e }
|
11
|
+
else
|
12
|
+
first.map.to_enum
|
13
|
+
end
|
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
|
17
|
+
obj = padded_first.zip *params
|
18
|
+
if block_given?
|
19
|
+
obj.map{|es| yield *es }
|
20
|
+
else
|
21
|
+
obj.map.to_enum
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# J-_-L
|
27
|
+
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
def instance_variables_from(obj, *only)
|
4
|
+
iter =
|
5
|
+
if obj.is_a? Binding
|
6
|
+
obj.eval('local_variables').map{|e| [obj.eval("#{e}"), e] }
|
7
|
+
elsif obj.is_a? Hash
|
8
|
+
obj.map{|k,v| [v,k] }
|
9
|
+
else
|
10
|
+
# elsif obj.is_a? Enumerable
|
11
|
+
obj.each.with_index
|
12
|
+
end
|
13
|
+
|
14
|
+
ret = []
|
15
|
+
iter.each{ |value, arg|
|
16
|
+
arg = arg.to_s
|
17
|
+
if only.include?(arg) || only.include?(arg.to_sym) || only.empty?
|
18
|
+
arg = '_' + arg if (48..57).member? arg.unpack('C')[0] # 1.8+1.9
|
19
|
+
ret << ivar = :"@#{arg}"
|
20
|
+
self.instance_variable_set ivar, value
|
21
|
+
end
|
22
|
+
}
|
23
|
+
ret
|
24
|
+
end
|
25
|
+
alias ivars instance_variables_from
|
26
|
+
|
27
|
+
# J-_-L
|
28
|
+
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
private
|
5
|
+
|
6
|
+
def activate_warnings!
|
7
|
+
$VERBOSE = true
|
8
|
+
end
|
9
|
+
|
10
|
+
def deactivate_warnings!
|
11
|
+
$VERBOSE = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def library?
|
15
|
+
caller[0].rindex(/:\d+(:in `.*')?$/)
|
16
|
+
$PROGRAM_NAME != $` # __FILE__
|
17
|
+
end
|
18
|
+
|
19
|
+
def executed_directly?
|
20
|
+
caller[0].rindex(/:\d+(:in `.*')?$/)
|
21
|
+
$PROGRAM_NAME == $` # __FILE__
|
22
|
+
end
|
23
|
+
alias standalone? executed_directly?
|
24
|
+
|
25
|
+
def irb?
|
26
|
+
!!(( IRB and $0 == 'irb' ) rescue nil)
|
27
|
+
end
|
28
|
+
|
29
|
+
def ignore_sigint! # ctrl+c
|
30
|
+
Signal.trap *%w|SIGINT IGNORE|
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# J-_-L
|
data/lib/zucker/7/mm.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
def m(levels = 1)
|
5
|
+
if self.is_a? Module
|
6
|
+
klass, method_function = self, :public_methods
|
7
|
+
else
|
8
|
+
klass, method_function = self.class, :public_instance_methods
|
9
|
+
|
10
|
+
eigen = self.singleton_methods
|
11
|
+
if !eigen.empty?
|
12
|
+
puts :Eigenclass # sorry for not being up to date, I just love the word
|
13
|
+
p self.singleton_methods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
levels.times{ |level|
|
18
|
+
if cur = klass.ancestors[level]
|
19
|
+
p cur # put class name
|
20
|
+
p cur.send method_function, false # put methods of the class
|
21
|
+
else
|
22
|
+
break
|
23
|
+
end
|
24
|
+
}
|
25
|
+
|
26
|
+
self # or whatever
|
27
|
+
end
|
28
|
+
|
29
|
+
alias mm m
|
30
|
+
alias method_list m
|
31
|
+
end
|
32
|
+
|
33
|
+
# J-_-L
|
34
|
+
|
data/lib/zucker/7/not.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
class Object
|
4
|
+
def not
|
5
|
+
NotClass.new self
|
6
|
+
end
|
7
|
+
|
8
|
+
class NotClass < BasicObject
|
9
|
+
def initialize(receiver)
|
10
|
+
@receiver = receiver
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(m, *args, &block)
|
14
|
+
not @receiver.public_send( m, *args, &block )
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# J-_-L
|
data/lib/zucker/7/oo.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
|
3
|
+
module Kernel
|
4
|
+
private
|
5
|
+
|
6
|
+
def o(desc = nil)
|
7
|
+
caller[0].rindex( /:(\d+)(:in (`.*'))?$/ )
|
8
|
+
m = $3 ? "method #$3, " : ""
|
9
|
+
d = desc ? "#{desc}: r" : 'R'
|
10
|
+
|
11
|
+
puts "#{d}eached #{m}line #$1 of file #$`"
|
12
|
+
# [$`, $1.to_i, $3.to_sym, desc]
|
13
|
+
end
|
14
|
+
alias oo o
|
15
|
+
end
|
16
|
+
|
17
|
+
# J-_-L
|
data/lib/zucker/7/os.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'zucker'
|
2
|
+
require 'rbconfig'
|
3
|
+
|
4
|
+
module OS
|
5
|
+
class << self
|
6
|
+
def is?(what)
|
7
|
+
what === RbConfig::CONFIG['host_os']
|
8
|
+
end
|
9
|
+
alias is is?
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
RbConfig::CONFIG['host_os']
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module_function
|
17
|
+
|
18
|
+
def linux?
|
19
|
+
OS.is?( /linux|cygwin/ )
|
20
|
+
end
|
21
|
+
|
22
|
+
def mac?
|
23
|
+
OS.is?( /mac|darwin/ )
|
24
|
+
end
|
25
|
+
|
26
|
+
def bsd?
|
27
|
+
OS.is?( /bsd/ )
|
28
|
+
end
|
29
|
+
|
30
|
+
def windows?
|
31
|
+
OS.is?( /mswin|mingw/ )
|
32
|
+
end
|
33
|
+
|
34
|
+
def solaris?
|
35
|
+
OS.is?( /solaris|sunos/ )
|
36
|
+
end
|
37
|
+
|
38
|
+
def posix?
|
39
|
+
linux? or mac? or bsd? or solaris? or begin
|
40
|
+
fork do end
|
41
|
+
true
|
42
|
+
rescue NotImplementedError, NoMethodError
|
43
|
+
false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
#def symbian?
|
48
|
+
#TODO who knows what symbian returns?
|
49
|
+
#end
|
50
|
+
|
51
|
+
# ...
|
52
|
+
end
|
53
|
+
|
54
|
+
# J-_-L
|
data/lib/zucker/7/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/7/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/os.rb
CHANGED
metadata
CHANGED
@@ -3,8 +3,8 @@ name: zucker
|
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
|
-
-
|
7
|
-
version: "
|
6
|
+
- 7
|
7
|
+
version: "7"
|
8
8
|
platform: ruby
|
9
9
|
authors:
|
10
10
|
- Jan Lelis
|
@@ -130,6 +130,47 @@ files:
|
|
130
130
|
- lib/zucker/iterate.rb
|
131
131
|
- lib/zucker/egonil.rb
|
132
132
|
- lib/zucker/mcopy.rb
|
133
|
+
- lib/zucker/7/default.rb
|
134
|
+
- lib/zucker/7/qq.rb
|
135
|
+
- lib/zucker/7/all.rb
|
136
|
+
- lib/zucker/7/string.rb
|
137
|
+
- lib/zucker/7/sandbox.rb
|
138
|
+
- lib/zucker/7/regexp2proc.rb
|
139
|
+
- lib/zucker/7/ivars.rb
|
140
|
+
- lib/zucker/7/dd.rb
|
141
|
+
- lib/zucker/7/class2proc.rb
|
142
|
+
- lib/zucker/7/enumerable.rb
|
143
|
+
- lib/zucker/7/alias_for.rb
|
144
|
+
- lib/zucker/7/control.rb
|
145
|
+
- lib/zucker/7/unary.rb
|
146
|
+
- lib/zucker/7/hash.rb
|
147
|
+
- lib/zucker/7/hash2proc.rb
|
148
|
+
- lib/zucker/7/tap.rb
|
149
|
+
- lib/zucker/7/blank.rb
|
150
|
+
- lib/zucker/7/not.rb
|
151
|
+
- lib/zucker/7/array2proc.rb
|
152
|
+
- lib/zucker/7/info.rb
|
153
|
+
- lib/zucker/7/array.rb
|
154
|
+
- lib/zucker/7/binding.rb
|
155
|
+
- lib/zucker/7/extensions.rb
|
156
|
+
- lib/zucker/7/mm.rb
|
157
|
+
- lib/zucker/7/oo.rb
|
158
|
+
- lib/zucker/7/aliases.rb
|
159
|
+
- lib/zucker/7/to_proc.rb
|
160
|
+
- lib/zucker/7/iterate.rb
|
161
|
+
- lib/zucker/7/egonil.rb
|
162
|
+
- lib/zucker/7/mcopy.rb
|
163
|
+
- lib/zucker/7/shortcuts.rb
|
164
|
+
- lib/zucker/7/square_brackets_for.rb
|
165
|
+
- lib/zucker/7/kernel.rb
|
166
|
+
- lib/zucker/7/engine.rb
|
167
|
+
- lib/zucker/7/os.rb
|
168
|
+
- lib/zucker/7/object.rb
|
169
|
+
- lib/zucker/7/cc.rb
|
170
|
+
- lib/zucker/7/debug.rb
|
171
|
+
- lib/zucker/7/env.rb
|
172
|
+
- lib/zucker/7/version.rb
|
173
|
+
- lib/zucker/7/union.rb
|
133
174
|
- lib/zucker/shortcuts.rb
|
134
175
|
- lib/zucker/square_brackets_for.rb
|
135
176
|
- lib/zucker/kernel.rb
|