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,12 @@
1
+ require 'zucker'
2
+
3
+ module Kernel
4
+ private
5
+
6
+ def q(*args)
7
+ puts args.map( &:inspect )*' ' unless args.empty?
8
+ end
9
+ alias qq q
10
+ end
11
+
12
+ # J-_-L
@@ -0,0 +1,12 @@
1
+ require 'zucker'
2
+
3
+ class Regexp
4
+ def to_proc
5
+ proc do |e|
6
+ e.to_s[self]
7
+ end
8
+ end
9
+ end
10
+
11
+ # J-_-L
12
+
@@ -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,4 @@
1
+ require 'zucker'
2
+ Zucker.require_this __FILE__
3
+
4
+ # J-_-L
@@ -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
+
@@ -0,0 +1,11 @@
1
+ require 'zucker'
2
+
3
+ def tap_on(obj, &block)
4
+ obj.tap &block
5
+ end
6
+
7
+ def make_new(what, *args, &block)
8
+ what.new(*args).tap &block
9
+ end
10
+
11
+ # J-_-L
@@ -0,0 +1,4 @@
1
+ require 'zucker'
2
+ Zucker.require_this __FILE__
3
+
4
+ # J-_-L
@@ -0,0 +1,24 @@
1
+ require 'zucker'
2
+
3
+ class String
4
+ def +@
5
+ self
6
+ end
7
+
8
+ def -@
9
+ to_sym
10
+ end
11
+ end
12
+
13
+ class Symbol
14
+ def +@
15
+ to_s
16
+ end
17
+
18
+ def -@
19
+ self
20
+ end
21
+ end
22
+
23
+ # J-_-L
24
+
@@ -0,0 +1,16 @@
1
+ require 'zucker'
2
+
3
+ class Regexp
4
+ def |(arg)
5
+ Regexp.union self, arg.is_a?(Regexp) ? arg : arg.to_s
6
+ end
7
+ end
8
+
9
+ class String
10
+ def |(arg)
11
+ Regexp.union self, arg.is_a?(Regexp) ? arg : arg.to_s
12
+ end
13
+ end
14
+
15
+ # J-_-L
16
+
@@ -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
@@ -25,10 +25,10 @@ block_given?
25
25
  - #{self.eval 'block_given?'}"
26
26
 
27
27
  end
28
-
29
- alias v binding
30
- alias vv binding
31
28
  end
32
29
 
30
+ alias v binding
31
+ alias vv binding
32
+
33
33
  # J-_-L
34
34
 
@@ -1,12 +1,23 @@
1
1
  require 'zucker'
2
2
 
3
- def egonil(nil_value = nil)
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
- rescue NoMethodError => e
6
- if e.message =~ /NilClass$/
7
- nil_value
8
- else
9
- raise NoMethodError
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
 
@@ -5,8 +5,7 @@ module RubyEngine
5
5
  @interpreter = case
6
6
  when RUBY_PLATFORM == 'parrot'
7
7
  'cardinal'
8
- when Object.constants.include?( :RUBY_ENGINE ) ||
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'
@@ -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
 
@@ -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
- padded_first = Array.new( [first, *params].max_by(&:size).size ){|i| first[i] } # append nils
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 }
@@ -8,7 +8,7 @@ module Kernel
8
8
  m = $3 ? "method #$3, " : ""
9
9
  d = desc ? "#{desc}: r" : 'R'
10
10
 
11
- puts ret = "#{d}eached #{m}line #$1 of file #$`"
11
+ puts "#{d}eached #{m}line #$1 of file #$`"
12
12
  # [$`, $1.to_i, $3.to_sym, desc]
13
13
  end
14
14
  alias oo o
@@ -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 Process.respond_to?(:fork)
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?