Class: Rubyc::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/rubyc/cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ CLI

Returns a new instance of CLI.



10
11
12
13
14
15
# File 'lib/rubyc/cli.rb', line 10

def initialize(*args)
  super
  libs = options[:require] ? options[:require].strip.split(":") : []
  libs.each { |lib| require lib }
  $stdout.sync = options[:sync]
end

Instance Method Details

#compactObject



118
119
120
# File 'lib/rubyc/cli.rb', line 118

def compact
  $stdin.each { |line| puts line if line.chomp! != "" }
end

#count_by(code = nil) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/rubyc/cli.rb', line 62

def count_by(code = nil)
  code ||= "line"
  proc = eval("Proc.new{|line| l = line; #{code}}")
  counts = $stdin.count_by do |line|
    proc.call(line.chomp)
  end
  puts counts.to_yaml
end

#grep(pattern, code = nil) ⇒ Object



84
85
86
87
88
# File 'lib/rubyc/cli.rb', line 84

def grep(pattern, code = nil)
  pattern = eval(pattern)
  proc = code ? eval("Proc.new{|line| l = line; #{code}}") : nil
  puts $stdin.grep(pattern, &proc)
end

#help(*args) ⇒ Object



17
18
19
# File 'lib/rubyc/cli.rb', line 17

def help(*args)
  super(*args)
end

#map(code) ⇒ Object



24
25
26
27
28
29
# File 'lib/rubyc/cli.rb', line 24

def map(code)
  proc = eval("Proc.new{|line,index| l = line; lnum = index + 1;#{code}}")
  $stdin.each_line.each_with_index do |line, index|
    puts proc.call(line.chomp, index).to_s
  end
end

#merge(nb_lines, sep = ",") ⇒ Object



124
125
126
# File 'lib/rubyc/cli.rb', line 124

def merge(nb_lines, sep = ",")
  $stdin.each_slice(nb_lines.to_i) { |chunk| puts chunk.map { |elem| elem.strip }.join(sep) }
end

#reject(code) ⇒ Object



53
54
55
56
57
58
# File 'lib/rubyc/cli.rb', line 53

def reject(code)
  proc = eval("Proc.new{|line,index| l = line; lnum = index + 1;#{code}}")
  $stdin.each_line.each_with_index do |line, index|
    puts line unless proc.call(line.chomp, index)
  end
end

#scan(pattern, code = nil) ⇒ Object



92
93
94
95
96
97
# File 'lib/rubyc/cli.rb', line 92

def scan(pattern, code = nil)
  pattern = eval(pattern)
  proc = code ? eval("Proc.new{|*match| m = match; #{code}}") : nil
  str = $stdin.read
  str.scan(pattern, &proc)
end

#select(code) ⇒ Object



44
45
46
47
48
49
# File 'lib/rubyc/cli.rb', line 44

def select(code)
  proc = eval("Proc.new{|line,index| l = line; lnum = index + 1;#{code}}")
  $stdin.each_line.each_with_index do |line, index|
    puts line if proc.call(line.chomp, index)
  end
end

#sort_by(code = nil) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/rubyc/cli.rb', line 73

def sort_by(code = nil)
  code ||= "line"
  proc = eval("Proc.new{|line| l = line; #{code}}")
  counts = $stdin.sort_by do |line|
    proc.call(line.chomp)
  end
  puts counts
end

#sum(code = nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/rubyc/cli.rb', line 33

def sum(code = nil)
  code ||= "line"
  proc = eval("Proc.new{|line| l = line; #{code}}")
  sum = $stdin.sum do |line|
    proc.call(line.chomp)
  end
  puts sum
end

#uniqObject



101
102
103
# File 'lib/rubyc/cli.rb', line 101

def uniq
  puts $stdin.to_a.uniq
end

#uniq_by(code = nil) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/rubyc/cli.rb', line 107

def uniq_by(code = nil)
  code ||= "line"
  proc = eval("Proc.new{|line| l = line; #{code}}")
  counts = $stdin.uniq_by do |line|
    proc.call(line.chomp)
  end
  puts counts
end