-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,22 @@ | ||
task :default do | ||
sh "rspec spec/" | ||
end | ||
|
||
begin | ||
require 'jeweler' | ||
Jeweler::Tasks.new do |gem| | ||
gem.name = 'pru' | ||
gem.summary = "Pipeable Ruby - forget about grep / sed / awk / wc ... use pure, readable Ruby!" | ||
gem.email = "[email protected]" | ||
|
||
Jeweler::Tasks.new { |gem| | ||
gem.name = 'pru' | ||
gem.summary = 'Pipeable Ruby - forget about grep/sed/awk/wc... use pure, readable Ruby!' | ||
gem.email = '[email protected]' | ||
gem.homepage = "http://github.com/grosser/#{gem.name}" | ||
gem.authors = ["Michael Grosser"] | ||
end | ||
gem.authors = ['Michael Grosser'] | ||
} | ||
|
||
Jeweler::GemcutterTasks.new | ||
rescue LoadError | ||
puts "Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler" | ||
puts 'Jeweler, or one of its dependencies, is not available. Install it with: gem install jeweler' | ||
end | ||
|
||
desc 'Run specs' | ||
task :spec do | ||
sh 'rspec spec/' | ||
end | ||
|
||
task :default => :spec |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,57 @@ | ||
#!/usr/bin/env ruby | ||
require 'rubygems' | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
blackwinter
Author
Member
|
||
|
||
require 'optparse' | ||
|
||
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') | ||
require 'pru' | ||
|
||
options = {} | ||
OptionParser.new do |opts| | ||
opts.banner = <<BANNER | ||
|
||
OptionParser.new { |opts| | ||
opts.banner = <<-EOS | ||
Pipeable Ruby | ||
Use ruby in your pipes, forget about grep / sed / awk / wc ... | ||
Use ruby in your pipes, forget about grep/sed/awk/wc... | ||
Map works on each line as String | ||
Reduce works on all lines as Array (optional or via -r) | ||
Map works on each line as String. | ||
Reduce works on all lines as Array (optional or via -r). | ||
Usage: | ||
something | pru 'map' ['reduce'] | ||
something | pru -r 'reduce' | ||
EOS | ||
|
||
opts.separator '' | ||
opts.separator 'Options:' | ||
|
||
opts.on('-r', '--reduce CODE', 'Reduce via CODE') { |code| | ||
options[:reduce] = code | ||
} | ||
|
||
opts.separator '' | ||
opts.separator 'Generic options:' | ||
|
||
opts.on('-h', '--help', 'Print this help message and exit') { | ||
puts opts | ||
exit | ||
} | ||
This comment has been minimized.
Sorry, something went wrong.
grosser
|
||
|
||
Options: | ||
BANNER | ||
opts.on("-r", "--reduce S","reduce via") {|s| options[:reduce] = s } | ||
opts.on("-h", "--help","Show this.") { puts opts; exit } | ||
opts.on('-v', '--version','Show Version'){ puts Pru::VERSION; exit} | ||
end.parse! | ||
opts.on('--version', 'Print program version and exit') { | ||
puts "#{File.basename($0)} v#{Pru::VERSION}" | ||
exit | ||
} | ||
}.parse! | ||
|
||
map, reduce = ARGV | ||
reduce ||= options[:reduce] | ||
map = nil if map and map.empty? | ||
|
||
if map and not reduce | ||
Pru.map($stdin, map){|x| puts x } | ||
Pru.map($stdin, map) { |x| puts x } | ||
elsif map and reduce | ||
results = [] | ||
Pru.map($stdin, map){|x| results << x } | ||
Pru.map($stdin, map) { |x| results << x } | ||
puts Pru.reduce(results, reduce) | ||
elsif reduce | ||
puts Pru.reduce($stdin.read.split("\n"), reduce) | ||
puts Pru.reduce($stdin.read.split($/), reduce) | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
blackwinter
Author
Member
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,2 @@ | ||
class Array | ||
|
||
# http://madeofcode.com/posts/74-ruby-core-extension-array-sum | ||
def sum(method = nil, &block) | ||
if block_given? | ||
raise ArgumentError, "You cannot pass a block and a method!" if method | ||
inject(0) { |sum, i| sum + yield(i) } | ||
elsif method | ||
inject(0) { |sum, i| sum + i.send(method) } | ||
else | ||
inject(0) { |sum, i| sum + i } | ||
end | ||
end unless method_defined?(:sum) | ||
|
||
def mean(method = nil, &block) | ||
sum(method, &block) / size.to_f | ||
end unless method_defined?(:mean) | ||
|
||
def grouped | ||
group_by { |x| x } | ||
end unless method_defined?(:grouped) | ||
|
||
def group_by | ||
hash = {} | ||
each { |x| hash[yield(x)] = x } | ||
hash | ||
end unless method_defined?(:group_by) | ||
|
||
end | ||
|
||
class Symbol | ||
|
||
def to_proc | ||
proc { |obj, *args| obj.send(self, *args) } | ||
end unless method_defined?(:to_proc) | ||
|
||
end | ||
require 'pru/core_ext/array' | ||
require 'pru/core_ext/symbol' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Array | ||
|
||
# http://madeofcode.com/posts/74-ruby-core-extension-array-sum | ||
def sum(method = nil) | ||
if block_given? | ||
raise ArgumentError, 'You cannot pass both a block and a method' if method | ||
inject(0) { |s, x| s + yield(x) } | ||
elsif method | ||
inject(0) { |s, x| s + x.send(method) } | ||
else | ||
inject(0) { |s, x| s + x } | ||
end | ||
end unless method_defined?(:sum) | ||
|
||
def mean(method = nil, &block) | ||
sum(method, &block) / size.to_f | ||
end unless method_defined?(:mean) | ||
|
||
def grouped | ||
group_by { |x| x } | ||
end unless method_defined?(:grouped) | ||
|
||
def group_by | ||
hash = {} | ||
each { |x| hash[yield(x)] = x } | ||
hash | ||
end unless method_defined?(:group_by) | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Symbol | ||
|
||
def to_proc | ||
proc { |obj, *args| obj.send(self, *args) } | ||
end unless method_defined?(:to_proc) | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,79 @@ | ||
require File.expand_path('spec/spec_helper') | ||
require File.expand_path('../spec_helper', __FILE__) | ||
|
||
describe Pru do | ||
it "has a VERSION" do | ||
Pru::VERSION.should =~ /^\d+\.\d+\.\d+$/ | ||
|
||
it 'has a VERSION' do | ||
Pru::VERSION.should =~ /\A\d+\.\d+\.\d+\z/ | ||
end | ||
|
||
describe 'map' do | ||
it "selects" do | ||
`ls -l | ./bin/pru 'include?("G")'`.split("\n").size.should == 2 | ||
|
||
it 'selects' do | ||
%x{ls -l | ./bin/pru 'include?("G")'}.split($/).size.should == 2 | ||
end | ||
|
||
it "can selects via regex" do | ||
`ls -l | ./bin/pru /G/`.split("\n").size.should == 2 | ||
it 'selects via regex' do | ||
%x{ls -l | ./bin/pru /G/}.split($/).size.should == 2 | ||
end | ||
|
||
it "can selects via i" do | ||
`cat spec/test.txt | ./bin/pru 'i'`.split("\n")[0...3].should == ["1","2","3"] | ||
it 'selects via i' do | ||
%x{cat spec/test.txt | ./bin/pru 'i'}.split($/)[0...3].should == %w[1 2 3] | ||
end | ||
|
||
it "maps" do | ||
`echo abc | ./bin/pru 'gsub(/a/,"b")'`.should == "bbc\n" | ||
it 'maps' do | ||
%x{echo abc | ./bin/pru 'gsub(/a/, "b")'}.should == "bbc\n" | ||
end | ||
|
||
it "selects and reduces" do | ||
`cat spec/test.txt | ./bin/pru 'include?("abc")' 'size'`.should == "3\n" | ||
it 'selects and reduces' do | ||
%x{cat spec/test.txt | ./bin/pru 'include?("abc")' 'size'}.should == "3\n" | ||
end | ||
|
||
it "can open files" do | ||
`echo spec/test.txt | ./bin/pru 'File.read(self)'`.should == File.read('spec/test.txt') | ||
it 'opens files' do | ||
%x{echo spec/test.txt | ./bin/pru 'File.read(self)'}.should == File.read('spec/test.txt') | ||
end | ||
|
||
it "can open preserves whitespaces" do | ||
`echo ' ab\tcd ' | ./bin/pru 'self'`.should == " ab\tcd \n" | ||
it 'open preserves whitespaces' do | ||
%x{echo ' ab\tcd ' | ./bin/pru 'self'}.should == " ab\tcd \n" | ||
end | ||
|
||
it "works with continuous input" do | ||
results = `ruby -e 'STDOUT.sync = true; puts 1; sleep 2; puts 1' | ./bin/pru 'Time.now.to_i'`.split("\n") | ||
it 'works with continuous input' do | ||
results = %x{ruby -e 'STDOUT.sync = true; puts 1; sleep 2; puts 1' | ./bin/pru 'Time.now.to_i'}.split($/) | ||
results.size.should == 2 | ||
results.uniq.size.should == 2 # called at a different time -> parses as you go | ||
end | ||
|
||
end | ||
|
||
describe 'reduce' do | ||
it "reduces" do | ||
`cat spec/test.txt | ./bin/pru -r 'size'`.should == "5\n" | ||
|
||
it 'reduces' do | ||
%x{cat spec/test.txt | ./bin/pru -r 'size'}.should == "5\n" | ||
end | ||
|
||
it "prints arrays as newlines" do | ||
`cat spec/test.txt | ./bin/pru -r 'self'`.should == File.read('spec/test.txt') | ||
it 'prints arrays as newlines' do | ||
%x{cat spec/test.txt | ./bin/pru -r 'self'}.should == File.read('spec/test.txt') | ||
end | ||
|
||
it "can sum" do | ||
`cat spec/test.txt | ./bin/pru -r 'sum(&:to_i)'`.should == "1212\n" | ||
it 'can sum' do | ||
%x{cat spec/test.txt | ./bin/pru -r 'sum(&:to_i)'}.should == "1212\n" | ||
end | ||
|
||
it "can mean" do | ||
`cat spec/test.txt | ./bin/pru -r 'mean(&:to_i)'`.should == "242.4\n" | ||
it 'can mean' do | ||
%x{cat spec/test.txt | ./bin/pru -r 'mean(&:to_i)'}.should == "242.4\n" | ||
end | ||
|
||
it "can grouped" do | ||
`cat spec/test.txt | ./bin/pru -r 'grouped.map{|a,b| b.size }'`.should include("2\n") | ||
it 'can grouped' do | ||
%x{cat spec/test.txt | ./bin/pru -r 'grouped.map { |a, b| b.size }'}.should include("2\n") | ||
end | ||
|
||
end | ||
|
||
describe 'map and reduce' do | ||
it "selects with empty string and reduces" do | ||
`cat spec/test.txt | ./bin/pru '' 'size'`.should == "5\n" | ||
|
||
it 'selects with empty string and reduces' do | ||
%x{cat spec/test.txt | ./bin/pru '' 'size'}.should == "5\n" | ||
end | ||
|
||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
$LOAD_PATH.unshift 'lib' | ||
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__) | ||
require 'pru' |
saves 10ms on my machine, but then users that need a gem have to do an additional require 'rubygems' ?