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.91 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.90 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.89 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.88 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.87 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.86 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.85 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.84 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.83 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.82 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.81 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.80 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.79 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.78 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.77 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.76 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.75 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.74 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.73 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.72 lib/eval_helper/ternary_operator.rb