Sha256: 7cc3cf9cb8b9763fc43a0990313dbf11c42d18768d4a184517a2a80e13e2f81a

Contents?: true

Size: 780 Bytes

Versions: 74

Compression:

Stored size: 780 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)
    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

74 entries across 74 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.150 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.149 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.148 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.147 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.146 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.145 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.144 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.143 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.142 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.141 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.140 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.139 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.138 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.137 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.136 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.135 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.134 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.133 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.132 lib/eval_helper/each_with_index_do_code.rb
tbpgr_utils-0.0.131 lib/eval_helper/each_with_index_do_code.rb