Refactor a Ruby switch statement when each case evaluates to the same value? -
suppose have ruby switch statement like:
x = 1 case name when "bob" = x when "joe" b = x when "tom" c = x ... end
what's best way refactor in one-liner?
update: renamed variables make clearer i'm getting at.
in case, can use eval
method, not "nice way" it.
name = "tom" a,b,c = [0]*3 config = {"joe" => "a", "bob" => "b", "tom" => "c"} x = 1 eval("#{config[name]}=#{x}") puts a, b, c # => 0 # => 0 # => 1
Comments
Post a Comment