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/CHANGELOG
CHANGED
@@ -1,3 +1,11 @@
|
|
1
|
+
2010-10-03 | Zucker 6
|
2
|
+
* no new cubes
|
3
|
+
* bugfix for OS.posix?
|
4
|
+
* small changes + bugfixes + doc improvements
|
5
|
+
* added two user methods to Info
|
6
|
+
* changed egonil semantics ( using method_missing, see http://rbjl.net/26/catch_nil.rb )
|
7
|
+
* bugfix for vv
|
8
|
+
|
1
9
|
2010-09-04 | Zucker 5
|
2
10
|
* debug edition - added two debug helpers: oo (output line, method, file) and cc (output method callstack)
|
3
11
|
* renamed cube D to dd add added more debug aliases (for mm and binding)
|
data/Rakefile
CHANGED
@@ -24,20 +24,21 @@ Spec::Rake::SpecTask.new('spec') do |t|
|
|
24
24
|
end
|
25
25
|
|
26
26
|
# gem
|
27
|
-
PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*' ]
|
28
27
|
spec = Gem::Specification.new do |s|
|
29
28
|
s.name = 'zucker'
|
30
29
|
s.version = Zucker::VERSION
|
31
30
|
s.date = Zucker::DATE
|
32
|
-
s.authors = ['Jan Lelis','and
|
31
|
+
s.authors = ['Jan Lelis','and others']
|
33
32
|
s.email = 'mail@janlelis.de'
|
34
33
|
s.summary = "Sweeten your Ruby code with this syntactic sugar :).
|
35
34
|
Adds a lot of little helpers that you do not want to miss again.
|
36
35
|
See http://rubyzucker.info"
|
36
|
+
s.description = s.summary
|
37
37
|
s.homepage = 'http://rubyzucker.info'
|
38
|
-
s.files =
|
38
|
+
s.files = FileList[ '[a-zA-Z]*', 'lib/**/*' ].to_a
|
39
39
|
s.require_paths = ["lib"]
|
40
40
|
s.required_ruby_version = '>= 1.8.7' # 1.9 recommended
|
41
|
+
s.add_development_dependency 'coderay'
|
41
42
|
end
|
42
43
|
|
43
44
|
Rake::GemPackageTask.new(spec) do |pkg|
|
data/lib/zucker.rb
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
module Zucker
|
4
4
|
# version and date get modified by the :prepare_release rake task
|
5
|
-
VERSION = '
|
6
|
-
DATE = '2010-
|
5
|
+
VERSION = '6'
|
6
|
+
DATE = '2010-10-03'
|
7
7
|
|
8
8
|
# cube list
|
9
9
|
PACKAGES = {
|
@@ -65,8 +65,17 @@ module Zucker
|
|
65
65
|
aliases.each{ |old, new|
|
66
66
|
Object.class_eval "alias #{new} #{old}; loaded_aliases << :#{new}" rescue nil
|
67
67
|
}
|
68
|
-
|
69
|
-
eval "::
|
68
|
+
|
69
|
+
#eval "::RV = RubyVersion; loaded_aliases << :RV" rescue nil
|
70
|
+
(
|
71
|
+
Object.const_set '::RV', RubyVersion
|
72
|
+
loaded_aliases << :RV
|
73
|
+
) rescue nil
|
74
|
+
#eval "::RE = RubyEngine; loaded_aliases << :RE" rescue nil
|
75
|
+
(
|
76
|
+
Object.const_set '::RE', RubyEngine
|
77
|
+
loaded_aliases << :RE
|
78
|
+
) rescue nil
|
70
79
|
|
71
80
|
loaded_aliases
|
72
81
|
end
|
@@ -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/6/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/6/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/6/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
|