Skip to content

Commit

Permalink
rename to Pru
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Apr 15, 2011
1 parent 37bcb0d commit aa34a08
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 70 deletions.
10 changes: 4 additions & 6 deletions Rakefile
@@ -1,14 +1,12 @@
task :default => :spec
require "rspec/core/rake_task"
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = '--backtrace --color'
task :default do
sh "rspec spec/"
end

begin
require 'jeweler'
Jeweler::Tasks.new do |gem|
gem.name = 'rup'
gem.summary = "Ruby pipe helper"
gem.name = 'pru'
gem.summary = "Pipeable Ruby"
gem.email = "michael@grosser.it"
gem.homepage = "http://github.com/grosser/#{gem.name}"
gem.authors = ["Michael Grosser"]
Expand Down
24 changes: 12 additions & 12 deletions Readme.md
@@ -1,48 +1,48 @@
Ruby pipe helper
Pipeable Ruby

Use ruby in your pipes, forget about grep / sed / awk / wc ...

Sometimes rup is longer, but its easier to read/debug/refactor
Sometimes pru is longer, but its easier to read/debug/refactor
and you only need to know pure ruby.

Install
=======
sudo gem install rup
sudo gem install pru

Usage
=====
Rup supports mapping and reducing.<br/><br/>
pru supports mapping and reducing.<br/><br/>
Map works on each line as String<br/>
Reduce works on all lines as Array<br/>

something | rup 'map' ['reduce']
something | rup -r 'reduce'
something | pru 'map' ['reduce']
something | pru -r 'reduce'

A few simple examples.<br/>

# grep --- all lines including foo
ls -al | grep foo
ls -al | rup 'include?("foo")'
ls -al | pru 'include?("foo")'

# grep --- all lines including foo but not grep
ps -ef | grep foo | grep -v grep
ps -ef | rup 'include?("foo") and not include?("rup")'
ps -ef | pru 'include?("foo") and not include?("pru")'

# awk --- return second work
ls -al | awk '{print $2}'
ls -al | rup 'split(" ")[1]'
ls -al | pru 'split(" ")[1]'

# awk --- count and average of all integers on second position
ls -al | awk '{ s += $2; } END {print "average" ,int(s/NR);print "count ",int(NR)}'
ls -al | rup 'split(" ")[1]' '"average #{mean(&:to_i)}\ncount #{size}"'
ls -al | pru 'split(" ")[1]' '"average #{mean(&:to_i)}\ncount #{size}"'

# wc --- count lines
ls -al | wc -l
ls -al | rup -r 'size'
ls -al | pru -r 'size'

# sed -- replace a 5 with five
ls -al | sed 's/5/five/'
ls -al | rup 'gsub(/5/,"five")'
ls -al | pru 'gsub(/5/,"five")'


Author
Expand Down
18 changes: 9 additions & 9 deletions bin/rup → bin/pru
Expand Up @@ -3,39 +3,39 @@ require 'rubygems'
require 'optparse'

$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
require 'rup'
require 'pru'

options = {}
OptionParser.new do |opts|
opts.banner = <<BANNER
Ruby pipe helper
Pipeable Ruby
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)
Usage:
something | rup 'map' ['reduce']
something | rup -r 'reduce'
something | pru 'map' ['reduce']
something | pru -r 'reduce'
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 Rup::VERSION; exit}
opts.on('-v', '--version','Show Version'){ puts Pru::VERSION; exit}
end.parse!

map, reduce = ARGV
reduce ||= options[:reduce]
map = nil if map and map.empty?

if map and not reduce
Rup.map($stdin, map){|x| puts x }
Pru.map($stdin, map){|x| puts x }
elsif map and reduce
results = []
Rup.map($stdin, map){|x| results << x }
puts Rup.reduce(results, reduce)
Pru.map($stdin, map){|x| results << x }
puts Pru.reduce(results, reduce)
elsif reduce
puts Rup.reduce($stdin.read.split("\n"), reduce)
puts Pru.reduce($stdin.read.split("\n"), reduce)
end
2 changes: 1 addition & 1 deletion lib/rup.rb → lib/pru.rb
@@ -1,4 +1,4 @@
class Rup
class Pru
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip

def self.map(io, code)
Expand Down
20 changes: 10 additions & 10 deletions rup.gemspec → pru.gemspec
Expand Up @@ -4,33 +4,33 @@
# -*- encoding: utf-8 -*-

Gem::Specification.new do |s|
s.name = %q{rup}
s.name = %q{pru}
s.version = "0.1.0"

s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Michael Grosser"]
s.date = %q{2011-04-15}
s.default_executable = %q{rup}
s.default_executable = %q{pru}
s.email = %q{michael@grosser.it}
s.executables = ["rup"]
s.executables = ["pru"]
s.files = [
"Gemfile",
"Gemfile.lock",
"Rakefile",
"Readme.md",
"VERSION",
"bin/rup",
"lib/rup.rb",
"rup.gemspec",
"spec/rup_spec.rb",
"bin/pru",
"lib/pru.rb",
"pru.gemspec",
"spec/pru_spec.rb",
"spec/spec_helper.rb"
]
s.homepage = %q{http://github.com/grosser/rup}
s.homepage = %q{http://github.com/grosser/pru}
s.require_paths = ["lib"]
s.rubygems_version = %q{1.4.2}
s.summary = %q{Ruby pipe helper}
s.summary = %q{Pipeable Ruby}
s.test_files = [
"spec/rup_spec.rb",
"spec/pru_spec.rb",
"spec/spec_helper.rb"
]

Expand Down
35 changes: 35 additions & 0 deletions spec/pru_spec.rb
@@ -0,0 +1,35 @@
require File.expand_path('spec/spec_helper')

describe Pru do
before :all do
@default = `ls -l | wc -l`
end

it "has a VERSION" do
Pru::VERSION.should =~ /^\d+\.\d+\.\d+$/
end

it "maps" do
`ls -l | ./bin/pru 'include?("G")'`.split("\n").size.should == 2
end

it "maps and reduces" do
`ls -l | ./bin/pru 'include?("G")' 'size'`.should == "2\n"
end

it "maps with empty string and reduces" do
`ls -l | ./bin/pru '' 'size'`.should == @default
end

it "reduces" do
`ls -l | ./bin/pru -r 'size'`.should == @default
end

it "can sum" do
`echo 5 | ./bin/pru -r 'sum(&:to_i)'`.should == "5\n"
end

it "can mean" do
`echo 5 | ./bin/pru -r 'mean(&:to_i)'`.should == "5.0\n"
end
end
31 changes: 0 additions & 31 deletions spec/rup_spec.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/spec_helper.rb
@@ -1,2 +1,2 @@
$LOAD_PATH.unshift 'lib'
require 'rup'
require 'pru'

0 comments on commit aa34a08

Please sign in to comment.