Skip to content

Instantly share code, notes, and snippets.

@serradura
Last active December 18, 2018 15:34
Show Gist options
  • Save serradura/c3ff443e8b7882e7b17b840404a4c4a4 to your computer and use it in GitHub Desktop.
Save serradura/c3ff443e8b7882e7b17b840404a4c4a4 to your computer and use it in GitHub Desktop.
Exemplo de rake task com Functional Objects (Rubyconf Brasil 2018)
class TaskArgumentsCalculator
def initialize(operator)
@operator = operator
end
def call(_task, args)
puts map_numbers(args).reduce(&@operator)
end
def to_proc
-> (*args) { call(*args) }
end
private
def map_numbers(args)
[:a, :b].map { |key| args[key].to_f }
end
end
SumTaskArgs = TaskArgumentsCalculator.new(:+)
SubtractTaskArgs = TaskArgumentsCalculator.new(:-)
desc 'adds two numbers'
task :sum, [:a, :b], &SumTaskArgs
desc 'subtract two numbers.'
task :sub, [:a, :b], &SubtractTaskArgs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment