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