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.71 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.70 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.69 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.68 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.67 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.66 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.65 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.64 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.63 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.62 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.61 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.60 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.59 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.58 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.57 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.56 lib/eval_helper/ternary_operator.rb