rubyc 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +8 -2
- data/features/rubyc.feature +2 -5
- data/lib/rubyc/cli.rb +25 -13
- data/lib/rubyc/version.rb +1 -1
- data/spec/cli_spec.rb +8 -1
- metadata +104 -74
data/README.md
CHANGED
@@ -4,13 +4,19 @@ Adds Ruby's power to the command line.
|
|
4
4
|
## Introduction
|
5
5
|
Sometimes we need to process files or streams at the bash prompt for filtering, parsing, calculating etc. Unix offers many tools for doing those actions: grep, sed, awk etc. However, their usage are not easy to remember besause of their cryptic syntax. They also use Unix regexes which are more limited than Ruby's ones.
|
6
6
|
|
7
|
-
The Ruby interpreter offers us
|
7
|
+
The Ruby interpreter offers us some command line options for processing files or pipes. The -p, -n options in combination of the -e option, allows us to process lines one at a time. Personnaly I never remember how to use them.
|
8
8
|
|
9
|
-
For this reason, I have created Rubyc, which stands for Ruby Command line.
|
9
|
+
For this reason, I have created Rubyc, which stands for Ruby Command line.
|
10
10
|
## Installation
|
11
11
|
```
|
12
12
|
gem install rubyc
|
13
13
|
```
|
14
|
+
## Usage
|
15
|
+
Rubys supports some Enumerable methods applied to each line to the stdin stream.
|
16
|
+
|
17
|
+
Rubyc gives you the __line__ variable and ıts shorter alias __l__ to represent the current line on which Rubyc is iterating.
|
18
|
+
|
19
|
+
The __index__ and __lnum__ variable are also available to you. The __index__ variable represents the index of the line starting at 0 whereas __lnum__ is the line number which starts at 1.
|
14
20
|
## Examples
|
15
21
|
### Use Case: Upcasing
|
16
22
|
Upcase what is comming from stdin.
|
data/features/rubyc.feature
CHANGED
@@ -1,8 +1,4 @@
|
|
1
|
-
Feature:
|
2
|
-
In order to get going on coding my awesome app
|
3
|
-
I want to have aruba and cucumber setup
|
4
|
-
So I don't have to do it myself
|
5
|
-
|
1
|
+
Feature: rubyc helps you with the shell
|
6
2
|
Scenario: App just runs
|
7
3
|
When I get help for "rubyc"
|
8
4
|
Then the exit status should be 0
|
@@ -16,3 +12,4 @@ Feature: My bootstrapped app kinda works
|
|
16
12
|
|count_by|
|
17
13
|
|uniq|
|
18
14
|
|merge|
|
15
|
+
|reject|
|
data/lib/rubyc/cli.rb
CHANGED
@@ -10,11 +10,15 @@ module Rubyc
|
|
10
10
|
super
|
11
11
|
libs = options[:require] ? options[:require].strip.split(":") : []
|
12
12
|
libs.each {|lib| require lib}
|
13
|
+
$stdout.sync = true
|
13
14
|
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
16
|
+
def help(*args)
|
17
|
+
super *args
|
18
|
+
end
|
19
|
+
|
20
|
+
desc "map BLOCK", "Enumerable#map"
|
21
|
+
long_desc "THIS IS A LONG DESCRIPTION"
|
18
22
|
def map(code)
|
19
23
|
proc = eval( "Proc.new{|line,index| l = line; lnum = index + 1;#{code}}" )
|
20
24
|
$stdin.each_line.each_with_index do |line, index|
|
@@ -22,17 +26,17 @@ module Rubyc
|
|
22
26
|
end
|
23
27
|
end
|
24
28
|
|
25
|
-
desc
|
29
|
+
desc "sum BLOCK", "Active Support Enumerable#sum"
|
26
30
|
def sum(code = nil)
|
27
31
|
code ||= "line"
|
28
32
|
proc = eval("Proc.new{|line| l = line; #{code}}")
|
29
33
|
sum = $stdin.sum do |line|
|
30
|
-
proc.call(line.chomp)
|
34
|
+
proc.call(line.chomp)
|
31
35
|
end
|
32
36
|
puts sum
|
33
37
|
end
|
34
38
|
|
35
|
-
desc
|
39
|
+
desc "select BLOCK", "Enumerable#select"
|
36
40
|
def select(code)
|
37
41
|
proc = eval( "Proc.new{|line,index| l = line; lnum = index + 1;#{code}}" )
|
38
42
|
$stdin.each_line.each_with_index do |line, index|
|
@@ -40,7 +44,15 @@ module Rubyc
|
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
43
|
-
desc
|
47
|
+
desc "reject BLOCK", "Enumerable#reject"
|
48
|
+
def reject(code)
|
49
|
+
proc = eval( "Proc.new{|line,index| l = line; lnum = index + 1;#{code}}" )
|
50
|
+
$stdin.each_line.each_with_index do |line, index|
|
51
|
+
puts line unless proc.call(line.chomp, index)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
desc "count_by BLOCK", "Count the number of lines that have the same property. The property is defined by the return value of the given the block"
|
44
56
|
def count_by(code = nil)
|
45
57
|
code ||= "line"
|
46
58
|
proc = eval("Proc.new{|line| l = line; #{code}}")
|
@@ -50,7 +62,7 @@ module Rubyc
|
|
50
62
|
puts counts.to_yaml
|
51
63
|
end
|
52
64
|
|
53
|
-
desc
|
65
|
+
desc "sort_by BLOCK", "Enumerable#sort_by"
|
54
66
|
def sort_by(code = nil)
|
55
67
|
code ||= "line"
|
56
68
|
proc = eval("Proc.new{|line| l = line; #{code}}")
|
@@ -60,14 +72,14 @@ module Rubyc
|
|
60
72
|
puts counts
|
61
73
|
end
|
62
74
|
|
63
|
-
desc
|
75
|
+
desc "grep BLOCK", "Enumerable#grep"
|
64
76
|
def grep(pattern, code = nil)
|
65
77
|
pattern = eval(pattern)
|
66
78
|
proc = code ? eval("Proc.new{|line| l = line; #{code}}") : nil
|
67
79
|
puts $stdin.grep(pattern, &proc)
|
68
80
|
end
|
69
81
|
|
70
|
-
desc
|
82
|
+
desc "scan MATCHER BLOCK", "String#scan"
|
71
83
|
def scan(pattern, code = nil)
|
72
84
|
pattern = eval(pattern)
|
73
85
|
proc = code ? eval("Proc.new{|*match| m = match; #{code}}") : nil
|
@@ -75,17 +87,17 @@ module Rubyc
|
|
75
87
|
str.scan(pattern, &proc)
|
76
88
|
end
|
77
89
|
|
78
|
-
desc
|
90
|
+
desc "uniq", "Enumerable#uniq"
|
79
91
|
def uniq
|
80
92
|
puts $stdin.to_a.uniq
|
81
93
|
end
|
82
94
|
|
83
|
-
desc
|
95
|
+
desc "compact", "Remove empty lines"
|
84
96
|
def compact
|
85
97
|
$stdin.each{ |line| puts line if line.chomp! != ""}
|
86
98
|
end
|
87
99
|
|
88
|
-
desc
|
100
|
+
desc "merge NB_LINES [SEPARATOR]", "Merge NB_LINES consecutive lines using SEPARATOR. If SEPARATOR is not given \',\' is used"
|
89
101
|
def merge(nb_lines, sep = ",")
|
90
102
|
$stdin.each_slice(nb_lines.to_i){|chunk| puts chunk.map{|elem| elem.strip}.join(sep)}
|
91
103
|
end
|
data/lib/rubyc/version.rb
CHANGED
data/spec/cli_spec.rb
CHANGED
@@ -22,11 +22,18 @@ describe "A rubyc cli" do
|
|
22
22
|
out_str.must_equal "Black Jack\n"
|
23
23
|
end
|
24
24
|
|
25
|
+
it "should reject line from stdin and send it to stdout" do
|
26
|
+
out_str = local_io("John Doe\nBlack Jack\nDr Dolittle\nCracker Jack") do
|
27
|
+
@cli.reject('l =~ /Jack/ && lnum == 2 && index == 1')
|
28
|
+
end
|
29
|
+
out_str.must_equal "John Doe\nDr Dolittle\nCracker Jack\n"
|
30
|
+
end
|
31
|
+
|
25
32
|
it "should sum line from stdin and send it to stdout" do
|
26
33
|
out_str = local_io("1\n2\nthird\n4") do
|
27
34
|
@cli.sum('l.to_i * 2')
|
28
35
|
end
|
29
|
-
out_str.must_equal "14
|
36
|
+
out_str.must_equal "14\n"
|
30
37
|
end
|
31
38
|
|
32
39
|
it "should sort by stdin and send the result to stdout" do
|
metadata
CHANGED
@@ -1,90 +1,117 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubyc
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 25
|
5
5
|
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
6
11
|
platform: ruby
|
7
|
-
authors:
|
12
|
+
authors:
|
8
13
|
- Martin Chabot
|
9
14
|
autorequire:
|
10
15
|
bindir: bin
|
11
16
|
cert_chain: []
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
+
|
18
|
+
date: 2012-09-19 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
17
23
|
none: false
|
18
|
-
requirements:
|
19
|
-
- -
|
20
|
-
- !ruby/object:Gem::Version
|
21
|
-
|
24
|
+
requirements:
|
25
|
+
- - ">="
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
hash: 3
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
version: "0"
|
31
|
+
requirement: *id001
|
32
|
+
name: thor
|
22
33
|
type: :runtime
|
34
|
+
- !ruby/object:Gem::Dependency
|
23
35
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
- !ruby/object:Gem::Dependency
|
26
|
-
name: rake
|
27
|
-
requirement: &70323542606320 !ruby/object:Gem::Requirement
|
36
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
28
37
|
none: false
|
29
|
-
requirements:
|
30
|
-
- -
|
31
|
-
- !ruby/object:Gem::Version
|
32
|
-
|
38
|
+
requirements:
|
39
|
+
- - ">="
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
hash: 3
|
42
|
+
segments:
|
43
|
+
- 0
|
44
|
+
version: "0"
|
45
|
+
requirement: *id002
|
46
|
+
name: rake
|
33
47
|
type: :development
|
48
|
+
- !ruby/object:Gem::Dependency
|
34
49
|
prerelease: false
|
35
|
-
version_requirements:
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: bundler
|
38
|
-
requirement: &70323542605440 !ruby/object:Gem::Requirement
|
50
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
39
51
|
none: false
|
40
|
-
requirements:
|
52
|
+
requirements:
|
41
53
|
- - ~>
|
42
|
-
- !ruby/object:Gem::Version
|
43
|
-
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 15
|
56
|
+
segments:
|
57
|
+
- 1
|
58
|
+
- 0
|
59
|
+
version: "1.0"
|
60
|
+
requirement: *id003
|
61
|
+
name: bundler
|
44
62
|
type: :development
|
63
|
+
- !ruby/object:Gem::Dependency
|
45
64
|
prerelease: false
|
46
|
-
version_requirements:
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: minitest
|
49
|
-
requirement: &70323542604880 !ruby/object:Gem::Requirement
|
65
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
50
66
|
none: false
|
51
|
-
requirements:
|
52
|
-
- -
|
53
|
-
- !ruby/object:Gem::Version
|
54
|
-
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
hash: 3
|
71
|
+
segments:
|
72
|
+
- 0
|
73
|
+
version: "0"
|
74
|
+
requirement: *id004
|
75
|
+
name: minitest
|
55
76
|
type: :development
|
77
|
+
- !ruby/object:Gem::Dependency
|
56
78
|
prerelease: false
|
57
|
-
version_requirements:
|
58
|
-
- !ruby/object:Gem::Dependency
|
59
|
-
name: aruba
|
60
|
-
requirement: &70323542604320 !ruby/object:Gem::Requirement
|
79
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
61
80
|
none: false
|
62
|
-
requirements:
|
63
|
-
- -
|
64
|
-
- !ruby/object:Gem::Version
|
65
|
-
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
hash: 3
|
85
|
+
segments:
|
86
|
+
- 0
|
87
|
+
version: "0"
|
88
|
+
requirement: *id005
|
89
|
+
name: aruba
|
66
90
|
type: :development
|
91
|
+
- !ruby/object:Gem::Dependency
|
67
92
|
prerelease: false
|
68
|
-
version_requirements:
|
69
|
-
- !ruby/object:Gem::Dependency
|
70
|
-
name: colorize
|
71
|
-
requirement: &70323542603880 !ruby/object:Gem::Requirement
|
93
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
72
94
|
none: false
|
73
|
-
requirements:
|
74
|
-
- -
|
75
|
-
- !ruby/object:Gem::Version
|
76
|
-
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
hash: 3
|
99
|
+
segments:
|
100
|
+
- 0
|
101
|
+
version: "0"
|
102
|
+
requirement: *id006
|
103
|
+
name: colorize
|
77
104
|
type: :development
|
78
|
-
prerelease: false
|
79
|
-
version_requirements: *70323542603880
|
80
105
|
description: Adds Ruby's powers to the command line
|
81
|
-
email:
|
106
|
+
email:
|
82
107
|
- chabotm@gmail.com
|
83
|
-
executables:
|
108
|
+
executables:
|
84
109
|
- rubyc
|
85
110
|
extensions: []
|
111
|
+
|
86
112
|
extra_rdoc_files: []
|
87
|
-
|
113
|
+
|
114
|
+
files:
|
88
115
|
- .gitignore
|
89
116
|
- Gemfile
|
90
117
|
- Guardfile
|
@@ -110,35 +137,38 @@ files:
|
|
110
137
|
- spec/spec_helper.rb
|
111
138
|
homepage: https://github.com/martinos/rubyc
|
112
139
|
licenses: []
|
140
|
+
|
113
141
|
post_install_message:
|
114
142
|
rdoc_options: []
|
115
|
-
|
143
|
+
|
144
|
+
require_paths:
|
116
145
|
- lib
|
117
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
147
|
none: false
|
119
|
-
requirements:
|
120
|
-
- -
|
121
|
-
- !ruby/object:Gem::Version
|
122
|
-
|
123
|
-
segments:
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
hash: 3
|
152
|
+
segments:
|
124
153
|
- 0
|
125
|
-
|
126
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
154
|
+
version: "0"
|
155
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
127
156
|
none: false
|
128
|
-
requirements:
|
129
|
-
- -
|
130
|
-
- !ruby/object:Gem::Version
|
131
|
-
|
132
|
-
segments:
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
hash: 3
|
161
|
+
segments:
|
133
162
|
- 0
|
134
|
-
|
163
|
+
version: "0"
|
135
164
|
requirements: []
|
165
|
+
|
136
166
|
rubyforge_project: rubyc
|
137
|
-
rubygems_version: 1.8.
|
167
|
+
rubygems_version: 1.8.11
|
138
168
|
signing_key:
|
139
169
|
specification_version: 3
|
140
170
|
summary: Adds Ruby's powers to the command line
|
141
|
-
test_files:
|
171
|
+
test_files:
|
142
172
|
- features/fixtures/file1.csv
|
143
173
|
- features/fixtures/file1.txt
|
144
174
|
- features/fixtures/file2.txt
|