Sha256: 41bc977b30a9ccdb4ddbd997bd442ec072022dbd0f4c5b845d6c2e7098fb4546

Contents?: true

Size: 819 Bytes

Versions: 11

Compression:

Stored size: 819 Bytes

Contents

# encoding: utf-8

module EvalHelper
  # create each with index do single line code, for eval
  #
  # ==== Examples
  #
  #   class EvalHelperEachWithIndexDoTest
  #     include EvalHelper
  #
  #     def hoge(hash)
  #       each_with_index_do_code(hash[:target], hash[:proc])
  #     end
  #   end
  #
  #   hash = {
  #     target: '[:a, :b]',
  #     proc: "puts \"\#{i}:\#{v}1\"\nputs \"\#{i}:\#{v}2\"\n",
  #   }
  #   EvalHelperEachWithIndexDoTest.new.hoge(hash) # => return "[:a, :b].each_with_index do |v, i|\n  puts \"\#{i}:\#{v}1\"\n  puts \"\#{i}:\#{v}2\"\nend"
  #
  def each_with_index_do_code(target, proc)
    "#{target}.each { |v, i|#{proc} }"
    indented = proc.split("\n").reduce([]) { |ret, v|ret << "  #{v}" ; ret }.join("\n")
    "#{target}.each_with_index do |v, i|\n#{indented}\nend"
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.76 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.75 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.74 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.73 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.72 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.71 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.70 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.69 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.68 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.67 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.66 lib/eval_helper/each_with_index_do_code.rb