lib/rubyc/cli.rb in rubyc-0.0.16 vs lib/rubyc/cli.rb in rubyc-0.0.17
- old
+ new
@@ -1,39 +1,10 @@
require 'yaml'
require 'thor'
+require 'rubyc/core_extensions'
module Rubyc
- module ::Enumerable
- def count_by
- self.inject({}) do |memo, elem|
- key = yield elem
- memo[key] ||= 0
- memo[key] += 1
- memo
- end
- end
-
- # This method was borrowed from ActiveSupport code
- def group_by
- self.inject({}) do |memo, elem|
- key = yield elem
- memo[key] ||= []
- memo[key] << elem
- memo
- end
- end
-
- # File activesupport/lib/active_support/core_ext/enumerable.rb, line 57
- def sum(identity = 0, &block)
- if block_given?
- map(&block).sum(identity)
- else
- inject { |sum, element| sum + element } || identity
- end
- end
- end
-
class CLI < Thor
class_option :require, :aliases => '-r'
def initialize(*args)
super
@@ -43,12 +14,12 @@
$stdout.sync = true
desc :map, "Apply Enumerable#map on each line"
def map(code)
- proc = eval( "Proc.new{|line| l = line; #{code}}" )
- $stdin.each do |line|
- puts proc.call(line.chomp).to_s
+ 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
desc :sum, "Calculate the sum of Numeric expressed on each line"
def sum(code = nil)