Sha256: 4f071a5c2c4a408e9fee35fbd86753b672c4e11dc64cfed681c89a7fa7238a2c

Contents?: true

Size: 1.24 KB

Versions: 57

Compression:

Stored size: 1.24 KB

Contents

# encoding: utf-8

module EvalHelper
  # create times logic strings, for eval
  #
  # ==== Examples
  #
  # single_line_proc case
  #
  #   class EvalHelperTimesTest
  #     include EvalHelper
  #
  #     def hoge(number, proc)
  #       times_code(number, proc)
  #     end
  #   end
  #
  #   hash = {
  #     number: 2,
  #     proc: 'puts "#{i}times"',
  #   }
  #   EvalHelperTimesTest.new.hoge(hash[:number], hash[:proc])
  #
  # return
  #
  #   2.times { |i| puts "#{i}times" }
  #
  # multi_line_proc case
  #
  #   class EvalHelperTimesTest
  #     include EvalHelper
  #
  #     def hoge(number, proc)
  #       times_code(number, proc)
  #     end
  #   end
  #
  #   hash = {
  #     number: 3,
  #     proc: 'puts "#{i}times"\nputs "#{i*2}times"',
  #   }
  #   EvalHelperTimesTest.new.hoge(hash[:number], hash[:proc])
  #
  # return
  #
  #   3.times do |i|
  #     puts "#{i}times"
  #     puts "#{i*2}times"
  #   end
  #
  def times_code(number, proc)
    return "#{number.to_s}.times { |i| #{proc} }" if proc.count('\n') == 0
    indented = proc.split('\n').reduce([]) { |ret, v|ret << "  #{v}" ; ret }.join("\n")
    <<-EOS
#{number.to_s}.times do |i|
#{indented}
end
    EOS
  end
end

Version data entries

57 entries across 57 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.116 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.115 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.114 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.113 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.112 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.111 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.110 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.109 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.108 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.107 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.106 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.105 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.104 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.103 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.102 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.101 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.100 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.99 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.98 lib/eval_helper/times_code.rb
tbpgr_utils-0.0.97 lib/eval_helper/times_code.rb