Sha256: a668b86d80038a64ff899e7e8cf4caf12b3327ecaa9d42a1ff96334035068e33

Contents?: true

Size: 1.56 KB

Versions: 96

Compression:

Stored size: 1.56 KB

Contents

# encoding: utf-8

module EvalHelper
  # create unless strings, for eval
  #
  # ==== Examples
  #
  # true case
  #
  #   class EvalHelperTernaryTest
  #     include EvalHelper
  #
  #     def hoge(hash)
  #       msg = hash[:input]
  #       code = \
  #         if hash[:ret]
  #           ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
  #         else
  #           ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
  #         end
  #       instance_eval code
  #     end
  #   end
  #
  #   hash = {
  #     input: "test",
  #     cond: "msg == 'test'",
  #     true_case: "true",
  #     false_case: "false",
  #     ret: "ret",
  #   }
  #   EvalHelperTernaryTest.new.hoge(hash) # => return 'true'
  #
  # false case
  #
  #   class EvalHelperTernaryTest
  #     include EvalHelper
  #
  #     def hoge(hash)
  #       msg = hash[:input]
  #       code = \
  #         if hash[:ret]
  #           ternary_operator(hash[:cond], hash[:true_case], hash[:false_case], hash[:ret])
  #         else
  #           ternary_operator(hash[:cond], hash[:true_case], hash[:false_case])
  #         end
  #       instance_eval code
  #     end
  #   end
  #
  #   hash = {
  #     input: "not_test",
  #     cond: "msg == 'test'",
  #     true_case: "true",
  #     false_case: "false",
  #     ret: "ret",
  #   }
  #   EvalHelperTernaryTest.new.hoge(hash) # => return 'false'
  #
  def ternary_operator(condition, true_case, false_case, ret = nil)
    ret = ret.nil? ? '' : "#{ret} = "
    "#{ret}#{condition} ? #{true_case} : #{false_case}"
  end
end

Version data entries

96 entries across 96 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.111 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.110 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.109 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.108 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.107 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.106 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.105 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.104 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.103 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.102 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.101 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.100 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.99 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.98 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.97 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.96 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.95 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.94 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.93 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.92 lib/eval_helper/ternary_operator.rb