Skip to content

Commit

Permalink
Code style/layout.
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Apr 21, 2011
1 parent d8ac6bb commit 667d0bf
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 99 deletions.
26 changes: 15 additions & 11 deletions Rakefile
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
46 changes: 31 additions & 15 deletions bin/pru
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.

Copy link
@grosser

grosser Apr 22, 2011

saves 10ms on my machine, but then users that need a gem have to do an additional require 'rubygems' ?

This comment has been minimized.

Copy link
@blackwinter

blackwinter Apr 22, 2011

Author Member

and what about the user that doesn't even have rubygems installed? this wasn't a performance optimization, i just try not to mess with $LOAD_PATH too much (which is what require 'rubygems' actually does).

This comment has been minimized.

Copy link
@grosser

grosser Apr 22, 2011

i think that users who not use rubygems should not be allowed to use tools that I maintain, they always complain an whine about stuff that the other 99% are fine with...


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.

Copy link
@grosser

grosser Apr 22, 2011

I find the 'before' version a lot more readable, you can see all options at the first look and I think 'after' its harder to spot whats going on

This comment has been minimized.

Copy link
@blackwinter

blackwinter Apr 22, 2011

Author Member

yeah, i guess it's just a matter of taste. i find the 'after' much more readable.


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.

Copy link
@grosser

grosser Apr 22, 2011

good idea, these damn \r users :D

This comment has been minimized.

Copy link
@blackwinter

blackwinter Apr 22, 2011

Author Member

not only that, but the $/ is the IRS -- that's what we wanna separate on, so use it. it's just a general habit (you see, this whole commit was only to make it align with my personal habits and preferences).

This comment has been minimized.

Copy link
@grosser

grosser Apr 22, 2011

rewriting a whole lot of stuff just to make em comply to your habits can make contributing to a project more difficult :)

This comment has been minimized.

Copy link
@blackwinter

blackwinter Apr 22, 2011

Author Member

sure. but as i said in another comment, i wasn't "contributing" per se. that's just the way i work.

end
12 changes: 8 additions & 4 deletions lib/pru.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
require 'pru/core_ext'

class Pru
VERSION = File.read( File.join(File.dirname(__FILE__),'..','VERSION') ).strip
module Pru

def self.map(io, code)
extend self

VERSION = File.read(File.join(File.dirname(__FILE__), '..', 'VERSION')).strip

def map(io, code)
String.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def _pru(i)
#{code}
Expand All @@ -24,12 +27,13 @@ def _pru(i)
end
end

def self.reduce(array, code)
def reduce(array, code)
Array.class_eval <<-RUBY, __FILE__, __LINE__ + 1
def _pru
#{code}
end
RUBY
array._pru
end

end
39 changes: 2 additions & 37 deletions lib/pru/core_ext.rb
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'
29 changes: 29 additions & 0 deletions lib/pru/core_ext/array.rb
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
7 changes: 7 additions & 0 deletions lib/pru/core_ext/symbol.rb
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
70 changes: 39 additions & 31 deletions spec/pru_spec.rb
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
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
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'

0 comments on commit 667d0bf

Please sign in to comment.