zucker 5 → 6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) hide show
  1. data/CHANGELOG +8 -0
  2. data/Rakefile +4 -3
  3. data/lib/zucker.rb +13 -4
  4. data/lib/zucker/6/alias_for.rb +21 -0
  5. data/lib/zucker/6/aliases.rb +51 -0
  6. data/lib/zucker/6/all.rb +4 -0
  7. data/lib/zucker/6/array.rb +25 -0
  8. data/lib/zucker/6/array2proc.rb +18 -0
  9. data/lib/zucker/6/binding.rb +34 -0
  10. data/lib/zucker/6/blank.rb +28 -0
  11. data/lib/zucker/6/cc.rb +25 -0
  12. data/lib/zucker/6/class2proc.rb +12 -0
  13. data/lib/zucker/6/control.rb +4 -0
  14. data/lib/zucker/6/dd.rb +23 -0
  15. data/lib/zucker/6/debug.rb +4 -0
  16. data/lib/zucker/6/default.rb +4 -0
  17. data/lib/zucker/6/egonil.rb +27 -0
  18. data/lib/zucker/6/engine.rb +68 -0
  19. data/lib/zucker/6/enumerable.rb +14 -0
  20. data/lib/zucker/6/env.rb +4 -0
  21. data/lib/zucker/6/extensions.rb +4 -0
  22. data/lib/zucker/6/hash.rb +27 -0
  23. data/lib/zucker/6/hash2proc.rb +16 -0
  24. data/lib/zucker/6/info.rb +192 -0
  25. data/lib/zucker/6/iterate.rb +27 -0
  26. data/lib/zucker/6/ivars.rb +28 -0
  27. data/lib/zucker/6/kernel.rb +34 -0
  28. data/lib/zucker/6/mcopy.rb +10 -0
  29. data/lib/zucker/6/mm.rb +34 -0
  30. data/lib/zucker/6/not.rb +19 -0
  31. data/lib/zucker/6/object.rb +4 -0
  32. data/lib/zucker/6/oo.rb +17 -0
  33. data/lib/zucker/6/os.rb +54 -0
  34. data/lib/zucker/6/qq.rb +12 -0
  35. data/lib/zucker/6/regexp2proc.rb +12 -0
  36. data/lib/zucker/6/sandbox.rb +25 -0
  37. data/lib/zucker/6/shortcuts.rb +4 -0
  38. data/lib/zucker/6/square_brackets_for.rb +21 -0
  39. data/lib/zucker/6/string.rb +54 -0
  40. data/lib/zucker/6/tap.rb +11 -0
  41. data/lib/zucker/6/to_proc.rb +4 -0
  42. data/lib/zucker/6/unary.rb +24 -0
  43. data/lib/zucker/6/union.rb +16 -0
  44. data/lib/zucker/6/version.rb +118 -0
  45. data/lib/zucker/binding.rb +3 -3
  46. data/lib/zucker/egonil.rb +17 -6
  47. data/lib/zucker/engine.rb +1 -2
  48. data/lib/zucker/info.rb +16 -1
  49. data/lib/zucker/iterate.rb +4 -3
  50. data/lib/zucker/oo.rb +1 -1
  51. data/lib/zucker/os.rb +11 -6
  52. metadata +227 -174
@@ -0,0 +1,4 @@
1
+ require 'zucker'
2
+ Zucker.require_this __FILE__
3
+
4
+ # J-_-L
@@ -0,0 +1,4 @@
1
+ require 'zucker'
2
+ Zucker.require_this __FILE__
3
+
4
+ # J-_-L
@@ -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,16 @@
1
+ require 'zucker'
2
+
3
+ class Hash
4
+ def to_proc
5
+ Proc.new{ |obj|
6
+ if self.member? obj
7
+ self[obj].to_proc.call obj
8
+ else
9
+ obj
10
+ end
11
+ }
12
+ end
13
+ end
14
+
15
+ # J-_-L
16
+
@@ -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
@@ -0,0 +1,10 @@
1
+ require 'zucker'
2
+
3
+ class Object
4
+ def mcopy
5
+ Marshal.load Marshal.dump self
6
+ end
7
+ end
8
+
9
+ # J-_-L
10
+
@@ -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
+
@@ -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
@@ -0,0 +1,4 @@
1
+ require 'zucker'
2
+ Zucker.require_this __FILE__
3
+
4
+ # J-_-L
@@ -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
@@ -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|win|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