Sha256: f5b79d10ff0704386f81d9fa8feffefbdd60c84d9b6ff87191ca4dd028af49d4

Contents?: true

Size: 799 Bytes

Versions: 1

Compression:

Stored size: 799 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([]) { |a, e|a << "  #{e}"; a }.join("\n")
    "#{target}.each_with_index do |v, i|\n#{indented}\nend"
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/eval_helper/each_with_index_do_code.rb