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

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -