pru 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (7) hide show
  1. data/Rakefile +1 -1
  2. data/Readme.md +12 -10
  3. data/VERSION +1 -1
  4. data/lib/pru.rb +16 -30
  5. data/pru.gemspec +3 -3
  6. data/spec/pru_spec.rb +15 -3
  7. metadata +5 -5
data/Rakefile CHANGED
@@ -6,7 +6,7 @@ begin
6
6
  require 'jeweler'
7
7
  Jeweler::Tasks.new do |gem|
8
8
  gem.name = 'pru'
9
- gem.summary = "Pipeable Ruby"
9
+ gem.summary = "Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!"
10
10
  gem.email = "michael@grosser.it"
11
11
  gem.homepage = "http://github.com/grosser/#{gem.name}"
12
12
  gem.authors = ["Michael Grosser"]
data/Readme.md CHANGED
@@ -1,9 +1,4 @@
1
- Pipeable Ruby
2
-
3
- Use ruby in your pipes, forget about grep / sed / awk / wc ...
4
-
5
- Sometimes pru is longer, but its easier to read/debug/refactor
6
- and you only need to know pure ruby.
1
+ Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!
7
2
 
8
3
  Install
9
4
  =======
@@ -18,17 +13,21 @@ Reduce works on all lines as Array<br/>
18
13
  something | pru 'map' ['reduce']
19
14
  something | pru -r 'reduce'
20
15
 
21
- A few simple examples.<br/>
16
+ ### Examples
22
17
 
23
18
  # grep --- all lines including foo
24
19
  ls -al | grep foo
25
- ls -al | pru 'include?("foo")'
20
+ ls -al | pru /foo/
26
21
 
27
- # grep --- all lines including foo but not grep
22
+ # grep --- all lines including current date
23
+ ls -al | ???
24
+ ls -al | pru 'include?(Time.now.strftime("%Y-%m-%d"))'
25
+
26
+ # grep --- all lines including foo but not self
28
27
  ps -ef | grep foo | grep -v grep
29
28
  ps -ef | pru 'include?("foo") and not include?("pru")'
30
29
 
31
- # awk --- return second work
30
+ # awk --- return second item
32
31
  ls -al | awk '{print $2}'
33
32
  ls -al | pru 'split(" ")[1]'
34
33
 
@@ -44,6 +43,9 @@ A few simple examples.<br/>
44
43
  ls -al | sed 's/5/five/'
45
44
  ls -al | pru 'gsub(/5/,"five")'
46
45
 
46
+ # every second line
47
+ ls -al | pru 'i % 2 == 0'
48
+
47
49
 
48
50
  Author
49
51
  ======
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/lib/pru.rb CHANGED
@@ -2,47 +2,33 @@ class Pru
2
2
  VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
3
3
 
4
4
  def self.map(io, code)
5
+ String.class_eval <<-RUBY, __FILE__, __LINE__ + 1
6
+ def _pru(i)
7
+ #{code}
8
+ end
9
+ RUBY
10
+
11
+ i = 1
5
12
  io.readlines.each do |line|
6
- result = line.instance_exec{ eval(code) }
13
+ result = line._pru(i)
7
14
  if result == true
8
15
  yield line
16
+ elsif result.is_a?(Regexp)
17
+ yield line if line =~ result
9
18
  elsif result
10
19
  yield result
11
20
  end
21
+ i += 1
12
22
  end
13
23
  end
14
24
 
15
25
  def self.reduce(array, code)
16
- array.instance_exec{ eval(code) }
17
- end
18
- end
19
-
20
- class Object
21
- unless defined? instance_exec # 1.9
22
- module InstanceExecMethods #:nodoc:
23
- end
24
- include InstanceExecMethods
25
-
26
- # Evaluate the block with the given arguments within the context of
27
- # this object, so self is set to the method receiver.
28
- #
29
- # From Mauricio's http://eigenclass.org/hiki/bounded+space+instance_exec
30
- def instance_exec(*args, &block)
31
- begin
32
- old_critical, Thread.critical = Thread.critical, true
33
- n = 0
34
- n += 1 while respond_to?(method_name = "__instance_exec#{n}")
35
- InstanceExecMethods.module_eval { define_method(method_name, &block) }
36
- ensure
37
- Thread.critical = old_critical
26
+ Array.class_eval <<-RUBY, __FILE__, __LINE__ + 1
27
+ def _pru
28
+ #{code}
38
29
  end
39
-
40
- begin
41
- send(method_name, *args)
42
- ensure
43
- InstanceExecMethods.module_eval { remove_method(method_name) } rescue nil
44
- end
45
- end
30
+ RUBY
31
+ array._pru
46
32
  end
47
33
  end
48
34
 
data/pru.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{pru}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Grosser"]
12
- s.date = %q{2011-04-15}
12
+ s.date = %q{2011-04-16}
13
13
  s.default_executable = %q{pru}
14
14
  s.email = %q{michael@grosser.it}
15
15
  s.executables = ["pru"]
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
28
28
  s.homepage = %q{http://github.com/grosser/pru}
29
29
  s.require_paths = ["lib"]
30
30
  s.rubygems_version = %q{1.4.2}
31
- s.summary = %q{Pipeable Ruby}
31
+ s.summary = %q{Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!}
32
32
  s.test_files = [
33
33
  "spec/pru_spec.rb",
34
34
  "spec/spec_helper.rb"
data/spec/pru_spec.rb CHANGED
@@ -9,15 +9,27 @@ describe Pru do
9
9
  Pru::VERSION.should =~ /^\d+\.\d+\.\d+$/
10
10
  end
11
11
 
12
- it "maps" do
12
+ it "selects" do
13
13
  `ls -l | ./bin/pru 'include?("G")'`.split("\n").size.should == 2
14
14
  end
15
15
 
16
- it "maps and reduces" do
16
+ it "can selects via regex" do
17
+ `ls -l | ./bin/pru /G/`.split("\n").size.should == 2
18
+ end
19
+
20
+ it "can selects via i" do
21
+ `ls -l | ./bin/pru 'i'`.split("\n")[0...3].should == ["1","2","3"]
22
+ end
23
+
24
+ it "maps" do
25
+ `echo abc | ./bin/pru 'gsub(/a/,"b")'`.should == "bbc\n"
26
+ end
27
+
28
+ it "selects and reduces" do
17
29
  `ls -l | ./bin/pru 'include?("G")' 'size'`.should == "2\n"
18
30
  end
19
31
 
20
- it "maps with empty string and reduces" do
32
+ it "selects with empty string and reduces" do
21
33
  `ls -l | ./bin/pru '' 'size'`.should == @default
22
34
  end
23
35
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pru
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 25
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 1
10
+ version: 0.1.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Grosser
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-15 00:00:00 +02:00
18
+ date: 2011-04-16 00:00:00 +02:00
19
19
  default_executable: pru
20
20
  dependencies: []
21
21
 
@@ -71,7 +71,7 @@ rubyforge_project:
71
71
  rubygems_version: 1.4.2
72
72
  signing_key:
73
73
  specification_version: 3
74
- summary: Pipeable Ruby
74
+ summary: Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!
75
75
  test_files:
76
76
  - spec/pru_spec.rb
77
77
  - spec/spec_helper.rb