zucker 1 → 2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/CHANGELOG +14 -0
  2. data/Rakefile +14 -4
  3. data/lib/zucker.rb +0 -4
  4. data/lib/zucker/2/D.rb +19 -0
  5. data/lib/zucker/2/aliases.rb +28 -0
  6. data/lib/zucker/2/all.rb +6 -0
  7. data/lib/zucker/2/array.rb +17 -0
  8. data/lib/zucker/2/array2proc.rb +16 -0
  9. data/lib/zucker/2/binding.rb +25 -0
  10. data/lib/zucker/2/blank.rb +26 -0
  11. data/lib/zucker/2/class2proc.rb +10 -0
  12. data/lib/zucker/2/debug.rb +4 -0
  13. data/lib/zucker/2/default.rb +25 -0
  14. data/lib/zucker/2/desc/D.yaml +23 -0
  15. data/lib/zucker/2/desc/aliases.yaml +16 -0
  16. data/lib/zucker/2/desc/array.yaml +14 -0
  17. data/lib/zucker/2/desc/array2proc.yaml +17 -0
  18. data/lib/zucker/2/desc/binding.yaml +10 -0
  19. data/lib/zucker/2/desc/blank.yaml +10 -0
  20. data/lib/zucker/2/desc/class2proc.yaml +12 -0
  21. data/lib/zucker/2/desc/egonil.yaml +17 -0
  22. data/lib/zucker/2/desc/enumerable.yaml +11 -0
  23. data/lib/zucker/2/desc/hash.yaml +14 -0
  24. data/lib/zucker/2/desc/hash2proc.yaml +11 -0
  25. data/lib/zucker/2/desc/info.yaml +12 -0
  26. data/lib/zucker/2/desc/iterate.yaml +17 -0
  27. data/lib/zucker/2/desc/ivars.yaml +20 -0
  28. data/lib/zucker/2/desc/kernel.yaml +13 -0
  29. data/lib/zucker/2/desc/mcopy.yaml +12 -0
  30. data/lib/zucker/2/desc/mm.yaml +12 -0
  31. data/lib/zucker/2/desc/regexp2proc.yaml +13 -0
  32. data/lib/zucker/2/desc/sandbox.yaml +19 -0
  33. data/lib/zucker/2/desc/square_brackets_for.yaml +23 -0
  34. data/lib/zucker/2/desc/string.yaml +13 -0
  35. data/lib/zucker/2/desc/unary.yaml +15 -0
  36. data/lib/zucker/2/desc/union.yaml +11 -0
  37. data/lib/zucker/2/doc/zucker_doc.html +1494 -0
  38. data/lib/zucker/2/doc/zucker_doc.rb +498 -0
  39. data/lib/zucker/2/egonil.rb +14 -0
  40. data/lib/zucker/2/enumerable.rb +12 -0
  41. data/lib/zucker/2/hash.rb +19 -0
  42. data/lib/zucker/2/hash2proc.rb +14 -0
  43. data/lib/zucker/2/info.rb +164 -0
  44. data/lib/zucker/2/iterate.rb +23 -0
  45. data/lib/zucker/2/ivars.rb +26 -0
  46. data/lib/zucker/2/kernel.rb +18 -0
  47. data/lib/zucker/2/mcopy.rb +9 -0
  48. data/lib/zucker/2/mm.rb +31 -0
  49. data/lib/zucker/2/regexp2proc.rb +10 -0
  50. data/lib/zucker/2/sandbox.rb +19 -0
  51. data/lib/zucker/2/spec/D_spec.rb +35 -0
  52. data/lib/zucker/2/spec/aliases_spec.rb +13 -0
  53. data/lib/zucker/2/spec/array2proc_spec.rb +12 -0
  54. data/lib/zucker/2/spec/array_spec.rb +23 -0
  55. data/lib/zucker/2/spec/binding_spec.rb +8 -0
  56. data/lib/zucker/2/spec/blank_spec.rb +20 -0
  57. data/lib/zucker/2/spec/class2proc_spec.rb +9 -0
  58. data/lib/zucker/2/spec/egonil_spec.rb +30 -0
  59. data/lib/zucker/2/spec/enumerable_spec.rb +8 -0
  60. data/lib/zucker/2/spec/hash2proc_spec.rb +12 -0
  61. data/lib/zucker/2/spec/hash_spec.rb +17 -0
  62. data/lib/zucker/2/spec/info_spec.rb +8 -0
  63. data/lib/zucker/2/spec/iterate_spec.rb +54 -0
  64. data/lib/zucker/2/spec/ivars_spec.rb +20 -0
  65. data/lib/zucker/2/spec/kernel_spec.rb +28 -0
  66. data/lib/zucker/2/spec/mcopy_spec.rb +13 -0
  67. data/lib/zucker/2/spec/mm_spec.rb +8 -0
  68. data/lib/zucker/2/spec/regexp2proc_spec.rb +9 -0
  69. data/lib/zucker/2/spec/sandbox_spec.rb +26 -0
  70. data/lib/zucker/2/spec/spec_helper.rb +18 -0
  71. data/lib/zucker/2/spec/square_brackets_for_spec.rb +59 -0
  72. data/lib/zucker/2/spec/string_spec.rb +29 -0
  73. data/lib/zucker/2/spec/unary_spec.rb +26 -0
  74. data/lib/zucker/2/spec/union_spec.rb +15 -0
  75. data/lib/zucker/2/square_brackets_for.rb +20 -0
  76. data/lib/zucker/2/string.rb +25 -0
  77. data/lib/zucker/2/unary.rb +22 -0
  78. data/lib/zucker/2/union.rb +14 -0
  79. data/lib/zucker/all.rb +2 -2
  80. data/lib/zucker/array2proc.rb +7 -1
  81. data/lib/zucker/debug.rb +3 -3
  82. data/lib/zucker/default.rb +1 -1
  83. data/lib/zucker/desc/D.yaml +1 -1
  84. data/lib/zucker/desc/array.yaml +1 -1
  85. data/lib/zucker/desc/array2proc.yaml +5 -1
  86. data/lib/zucker/desc/binding.yaml +2 -2
  87. data/lib/zucker/desc/blank.yaml +1 -3
  88. data/lib/zucker/desc/egonil.yaml +1 -1
  89. data/lib/zucker/desc/enumerable.yaml +1 -1
  90. data/lib/zucker/desc/hash.yaml +4 -2
  91. data/lib/zucker/desc/info.yaml +12 -0
  92. data/lib/zucker/desc/kernel.yaml +2 -2
  93. data/lib/zucker/desc/mm.yaml +3 -3
  94. data/lib/zucker/desc/regexp2proc.yaml +1 -1
  95. data/lib/zucker/desc/sandbox.yaml +6 -1
  96. data/lib/zucker/desc/string.yaml +1 -1
  97. data/lib/zucker/desc/unary.yaml +2 -2
  98. data/lib/zucker/desc/union.yaml +1 -1
  99. data/lib/zucker/doc/zucker_doc.html +441 -233
  100. data/lib/zucker/doc/zucker_doc.rb +32 -27
  101. data/lib/zucker/hash.rb +3 -3
  102. data/lib/zucker/info.rb +164 -0
  103. data/lib/zucker/ivars.rb +1 -1
  104. data/lib/zucker/kernel.rb +2 -4
  105. data/lib/zucker/sandbox.rb +7 -3
  106. data/lib/zucker/spec/D_spec.rb +15 -7
  107. data/lib/zucker/spec/aliases_spec.rb +3 -24
  108. data/lib/zucker/spec/array2proc_spec.rb +5 -1
  109. data/lib/zucker/spec/array_spec.rb +1 -1
  110. data/lib/zucker/spec/binding_spec.rb +4 -2
  111. data/lib/zucker/spec/blank_spec.rb +1 -1
  112. data/lib/zucker/spec/class2proc_spec.rb +1 -1
  113. data/lib/zucker/spec/egonil_spec.rb +1 -1
  114. data/lib/zucker/spec/enumerable_spec.rb +1 -1
  115. data/lib/zucker/spec/hash2proc_spec.rb +1 -1
  116. data/lib/zucker/spec/hash_spec.rb +10 -3
  117. data/lib/zucker/spec/info_spec.rb +8 -0
  118. data/lib/zucker/spec/iterate_spec.rb +1 -1
  119. data/lib/zucker/spec/ivars_spec.rb +1 -1
  120. data/lib/zucker/spec/kernel_spec.rb +1 -7
  121. data/lib/zucker/spec/mcopy_spec.rb +1 -1
  122. data/lib/zucker/spec/mm_spec.rb +4 -2
  123. data/lib/zucker/spec/regexp2proc_spec.rb +1 -1
  124. data/lib/zucker/spec/sandbox_spec.rb +22 -3
  125. data/lib/zucker/spec/spec_helper.rb +18 -0
  126. data/lib/zucker/spec/square_brackets_for_spec.rb +1 -1
  127. data/lib/zucker/spec/string_spec.rb +7 -7
  128. data/lib/zucker/spec/unary_spec.rb +1 -1
  129. data/lib/zucker/spec/union_spec.rb +8 -6
  130. metadata +207 -131
  131. data/lib/zucker/doc/meta.yaml +0 -4
@@ -0,0 +1,14 @@
1
+ 2010-08-08 | Zucker 2
2
+ * added info cube
3
+ * added chaining for array2proc
4
+ * fixed Hash.zip
5
+ * fixed instance_variables_from binding for 1.9
6
+ * more specs
7
+
8
+
9
+ 2010-08-06 | Zucker 1
10
+ * initial release
11
+
12
+
13
+ 2010-08-05 | Zucker 0
14
+ * pre-release for rug-b talk
data/Rakefile CHANGED
@@ -1,17 +1,27 @@
1
1
  require 'rake'
2
+ # require 'rubygems'
2
3
  require 'rake/gempackagetask'
3
- require 'rubygems'
4
4
  require 'spec/rake/spectask'
5
5
 
6
+ desc 'Build documentation'
7
+ task 'doc' do
8
+ zucker_path = "#{ Rake.application.find_rakefile_location[1] }/lib/zucker"
9
+ ruby File.join(zucker_path, 'doc/zucker_doc.rb'), zucker_path
10
+ end
11
+
12
+ Spec::Rake::SpecTask.new('spec') do |t|
13
+ t.spec_files = FileList['lib/zucker/spec/**/*.rb']
14
+ end
15
+
6
16
  # gem
7
17
  PKG_FILES = FileList[ '[a-zA-Z]*', 'lib/**/*' ]
8
18
  spec = Gem::Specification.new do |s|
9
19
  s.name = 'zucker'
10
- s.version = '1'
11
- s.date = '2010-08-05'
20
+ s.version = '2'
21
+ s.date = '2010-08-08'
12
22
  s.authors = ['Jan Lelis']
13
23
  s.email = 'mail@janlelis.de'
14
- s.summary = 'Add more sugar to Ruby'
24
+ s.summary = 'Make your Ruby code even sweeter with this syntacitcal sugar: http://rubyzucker.info :)'
15
25
  s.homepage = 'http://rubyzucker.info'
16
26
  s.files = PKG_FILES.to_a
17
27
  s.require_paths = [".","lib"]
@@ -1,7 +1,3 @@
1
- module Zucker
2
- VERSION = '1'
3
- end
4
-
5
1
  # nothing is done here
6
2
  # to use zucker:
7
3
 
@@ -0,0 +1,19 @@
1
+ module Kernel
2
+ def D(*args, &block)
3
+ if args.empty?
4
+ tap{
5
+ if block_given?
6
+ puts yield self
7
+ else
8
+ puts self.inspect
9
+ end
10
+ }
11
+ else
12
+ 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!"
13
+ # eval ...
14
+ end
15
+ end
16
+ end
17
+
18
+ # J-_-L
19
+
@@ -0,0 +1,28 @@
1
+ alias is_an? is_a?
2
+
3
+ module Enumerable
4
+ alias with zip
5
+ alias % zip
6
+ end
7
+
8
+ class Array
9
+ alias ** product
10
+ end
11
+
12
+ class Hash
13
+ alias + merge
14
+ end
15
+
16
+ class Binding
17
+ #alias [] eval
18
+ def [](expr)
19
+ self.eval "#{expr}"
20
+ end
21
+ end
22
+
23
+ # constants - who would use these in real-world code for other things?
24
+ Infinity = 1.0 / 0.0 # or 2*Float::MAX
25
+ NaN = 0.0 / 0.0
26
+
27
+ # J-_-L
28
+
@@ -0,0 +1,6 @@
1
+ # require all the sugar ;)
2
+ # see README for an explaination of what they do
3
+
4
+ require 'zucker/default'
5
+ require 'zucker/debug'
6
+
@@ -0,0 +1,17 @@
1
+ class Array
2
+ def ^(other) # TODO: more efficient
3
+ (self - other) +
4
+ (other - self)
5
+ end
6
+
7
+ def sum # sry, you just need this one too often
8
+ inject :+
9
+ end
10
+
11
+ def chrs
12
+ self.pack 'C*'
13
+ end
14
+ end
15
+
16
+ # J-_-L
17
+
@@ -0,0 +1,16 @@
1
+ class Array
2
+ def to_proc
3
+ Proc.new{ |obj|
4
+ if self.first.is_a? Array
5
+ self.inject(obj){ |result, nested_array|
6
+ nested_array.to_proc.call result
7
+ }
8
+ else
9
+ obj.send *self
10
+ end
11
+ }
12
+ end
13
+ end
14
+
15
+ # J-_-L
16
+
@@ -0,0 +1,25 @@
1
+ class Binding
2
+ def inspect
3
+ put_vars = lambda { |array|
4
+ if array.empty?
5
+ ' - none'
6
+ else
7
+ array.map{|e| " - #{e}: #{ self.eval '#{e}'}"}*"\n"
8
+ end
9
+ }
10
+
11
+ "#{self.to_s}
12
+ local vars
13
+ #{ put_vars[ self.eval 'local_variables' ] }
14
+ (instance vars)
15
+ #{ put_vars[ self.eval 'instance_variables' ] }
16
+ self
17
+ - #{self.eval 'self'}
18
+ block_given?
19
+ - #{self.eval 'block_given?'}"
20
+
21
+ end
22
+ end
23
+
24
+ # J-_-L
25
+
@@ -0,0 +1,26 @@
1
+ class Object
2
+ def blank?
3
+ if respond_to? :empty? then empty? else !self end
4
+ end
5
+
6
+ def present?
7
+ !blank?
8
+ end
9
+ end
10
+
11
+
12
+ { # what to do # for which classes
13
+ lambda{ true } => [FalseClass, NilClass],
14
+ lambda{ false } => [TrueClass, Numeric],
15
+ lambda{ empty? } => [Array, Hash],
16
+ lambda{ self !~ /\S/ } => [String],
17
+ lambda{ self == // } => [Regexp],
18
+
19
+ }.each{ |action, klass_array|
20
+ klass_array.each{ |klass|
21
+ klass.send :define_method, :blank?, &action
22
+ }
23
+ }
24
+
25
+ # J-_-L
26
+
@@ -0,0 +1,10 @@
1
+ class Class
2
+ def to_proc
3
+ Proc.new do |*args|
4
+ self.new *args
5
+ end
6
+ end
7
+ end
8
+
9
+ # J-_-L
10
+
@@ -0,0 +1,4 @@
1
+ require 'zucker/D'
2
+ require 'zucker/mm'
3
+ require 'zucker/binding'
4
+
@@ -0,0 +1,25 @@
1
+ # default
2
+ zucker = %w|
3
+ aliases
4
+ array
5
+ array2proc
6
+ blank
7
+ class2proc
8
+ egonil
9
+ enumerable
10
+ hash
11
+ hash2proc
12
+ iterate
13
+ ivars
14
+ kernel
15
+ mcopy
16
+ regexp2proc
17
+ sandbox
18
+ square_brackets_for
19
+ string
20
+ unary
21
+ union
22
+ |
23
+
24
+ zucker.each{|rb| require "zucker/#{rb}"}
25
+
@@ -0,0 +1,23 @@
1
+ D:
2
+ summary: Easy debug printing with the ⇧p⇧ alternative ⇧.D⇧. It outputs to stdout and returns ⇧self⇧. Accepts a block.
3
+ methods:
4
+ Object#D: |
5
+ some.D.methods.D.noone.D.knows.D
6
+ # ...outputs 4 lines with the inspected objects
7
+ # => (result)
8
+
9
+ 21+Math.sin(42).D
10
+ # outputs -0.916521547915634
11
+ # => 20.0834784520844
12
+
13
+ name = 'Earth'
14
+ 'Hello ' + name.D{|e| "The length is: #{e.size}"}
15
+ # outputs "The length is: 5"
16
+ # => 'Hello Earth'
17
+ package: debug
18
+ info:
19
+ - "This is inspired by the funny →⇧.tap⇧ method→http://moonbase.rydia.net/mental/blog/programming/eavesdropping-on-expressions→."
20
+ versions:
21
+ - 1.9
22
+ - 1.8
23
+
@@ -0,0 +1,16 @@
1
+ aliases:
2
+ summary: Some convenient aliases (and constants) for existing methods.
3
+ package: default
4
+ methods:
5
+ Object#is_an?: Object.is_a?
6
+ Enumerable#with: Enumerable#zip
7
+ Enumerable#%: Enumerable#zip
8
+ Array#**: Array#product
9
+ Hash#+: Hash#merge
10
+ Binding#[]: Binding#eval
11
+ Infinity: 1.0 / 0.0
12
+ NaN: 0.0 / 0.0
13
+ versions:
14
+ - 1.9
15
+ - 1.8
16
+
@@ -0,0 +1,14 @@
1
+ array:
2
+ summary: Methods one could miss for ⇧Array⇧.
3
+ why:
4
+ - ⇧Array⇧ has ⇧&⇧ and ⇧|⇧, but why does it not have ⇧^⇧?
5
+ - ⇧sum⇧ is just an alias for ⇧inject(:+)⇧, but it is needed pretty often..
6
+ methods:
7
+ Array#^: "[1,2,3,4] ^ [3,4,5,6] # => [1,2,5,6]"
8
+ Array#sum: "[1,2,3,4,5].sum # => 15"
9
+ Array#chrs: "[72, 97, 108, 108, 111].chrs # => 'Hallo'"
10
+ package: default
11
+ versions:
12
+ - 1.9
13
+ - 1.8
14
+
@@ -0,0 +1,17 @@
1
+ array2proc:
2
+ summary: Calls the method named by the first paramenter and passes the other elements as paramaters.
3
+ why: When using ⇧Symbol#to_proc⇧, you often wish to pass parameters.
4
+ methods:
5
+ Array#to_proc: |
6
+ [1,2,3,4].map &[:*, 5] # => [5, 10, 15, 20]
7
+ # you can also chain them, if the first parameter is an Array
8
+ [1,2,3,4].map &[[:to_s, 2],[:+, 'b']] # => ["1b", "10b", "11b", "100b"]
9
+ info:
10
+ - Inspired by →this article→http://www.sanityinc.com/articles/adding-array-to-proc-to-ruby→.
11
+ - "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
12
+ - Chaining inspired by →eregon→http://github.com/eregon/Classes/blob/master/to_proc.rb→.
13
+ package: default
14
+ versions:
15
+ - 1.9
16
+ - 1.8
17
+
@@ -0,0 +1,10 @@
1
+ binding:
2
+ summary: Adds a verbose ⇧Binding#inspect⇧.
3
+ why: It gives you information about the current environment.
4
+ methods:
5
+ Binding#inspect: binding.inspect
6
+ package: debug
7
+ versions:
8
+ - 1.9
9
+ - 1.8
10
+
@@ -0,0 +1,10 @@
1
+ blank:
2
+ summary: Does pretty the same as in ActiveSupport (Every object can be asked if it is blank).
3
+ why: It's too familiar ;)
4
+ methods:
5
+ Object#blank?: "'an object'.blank? # => false"
6
+ package: default
7
+ versions:
8
+ - 1.9
9
+ - 1.8
10
+
@@ -0,0 +1,12 @@
1
+ class2proc:
2
+ summary: Creates a new instance of the class.
3
+ methods:
4
+ Class#to_proc: "[ [1,2],[3,5,6,7,3] ].map(&Set) # => [ Set[1,2], Set[5,6,7,3] ]"
5
+ package: default
6
+ info:
7
+ - Inspired by Ruby Facets.
8
+ - "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
9
+ versions:
10
+ - 1.9
11
+ - 1.8
12
+
@@ -0,0 +1,17 @@
1
+ egonil:
2
+ summary: Creates a block, where ⇧nil⇧ does not raise ⇧NoMethodError⇧s.
3
+ methods:
4
+ egonil, nn: |
5
+ egonil do
6
+ nil.some_methods.that[:do].not.exist
7
+ end # => nil
8
+
9
+ nn(5){ nil.some_methods.that[:do].not.exist }
10
+ # => 5
11
+ package: default
12
+ info:
13
+ - See →this post→http://rbjl.net/26-the-28-bytes-of-ruby-joy→ for more information and discussion.
14
+ versions:
15
+ - 1.9
16
+ - 1.8
17
+
@@ -0,0 +1,11 @@
1
+ enumerable:
2
+ summary: ⇧Enumerable⇧ extensions.
3
+ methods:
4
+ Enumerable#mash: "[1,2,3].mash{|e| [e, e.to_s] } # => {1=>'1',2=>'2',3=>'3'}"
5
+ package: default
6
+ info:
7
+ - Inspired by Ruby Facets' ⇧mash⇧.
8
+ versions:
9
+ - 1.9
10
+ - 1.8
11
+
@@ -0,0 +1,14 @@
1
+ hash:
2
+ summary: Some sugar for dealing with hashs.
3
+ methods:
4
+ Hash.zip: "Hash.zip [1,2,3], [4,5,6] # => {1 => 4, 2 => 5, 3 => 6}"
5
+ Hash#<<: |
6
+ {1 => 2} << [3, 4] # => { 1 => 2, 3 => 4 }
7
+ {1 => 2} << { 5=>6 } # => { 1 => 2, 5 => 6 }
8
+ package: default
9
+ info:
10
+ - Some of the operators are inspired by Ruby Facets.
11
+ versions:
12
+ - 1.9
13
+ - 1.8
14
+
@@ -0,0 +1,11 @@
1
+ hash2proc:
2
+ summary: Use a hash to apply procs to specific objects.
3
+ methods:
4
+ Hash#to_proc: "[1,2,3,4].map(&{\n2 => lambda {|e| e + 1000},\n4 => :to_s,\n}) # => [1, 1002, 3, '4']"
5
+ package: default
6
+ info:
7
+ - "More about →to_proc→http://rbjl.net/29-become-a-proc-star→."
8
+ versions:
9
+ - 1.9
10
+ - 1.8
11
+
@@ -0,0 +1,12 @@
1
+ info:
2
+ summary: Access environment information with the ⇧Info⇧ module.
3
+ why: "You don't need to remember in which global variable, constant or special method the information you are searching for is hidden."
4
+ methods:
5
+ Info: |
6
+ # see the source file for the list of accessors
7
+ # you could also add them to the global namespace with: include Info
8
+ package: default
9
+ versions:
10
+ - 1.9
11
+ - 1.8 (not all information)
12
+
@@ -0,0 +1,17 @@
1
+ iterate:
2
+ summary: Iterate over one or more collections.
3
+ why: "It's like ⇧.each⇧ with two differences: It feels more like a control structure and you can easily iterate over multiple objects."
4
+ methods:
5
+ iterate: |
6
+ iterate [1,2], [3,4,5] do |e,f|
7
+ puts "#{e},#{f}"
8
+ end
9
+ # outputs
10
+ # 1,3
11
+ # 2,4
12
+ # ,5
13
+ package: default
14
+ versions:
15
+ - 1.9
16
+ - 1.8
17
+