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.151 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.150 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.149 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.148 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.147 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.146 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.145 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.144 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.143 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.142 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.141 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.140 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.139 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.138 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.137 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.136 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.135 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.134 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.133 lib/eval_helper/ternary_operator.rb
tbpgr_utils-0.0.132 lib/eval_helper/ternary_operator.rb