2

Possible Duplicate:
In Ruby, how do I find out if a string is not in an array?

Right now I have this code:

partial_update = true if session[:intended_params][:controller] == 'foo'
partial_update = true if session[:intended_params][:controller] == 'bar'
partial_update = true if session[:intended_params][:controller] == 'ding'
partial_update = true if session[:intended_params][:controller] == 'dong'
partial_update = true if session[:intended_params][:controller] == 'up'
partial_update = true if session[:intended_params][:controller] == 'down'
partial_update = true if session[:intended_params][:controller] == 'bing'
# ETC. 

I would like to change this to

array = %w[foo bar ding dong up down bing]
partial_update = true if session[:intended_params][:controller]  == array.any_of_the_possibilites

Is there a good way to do this?

0

2 Answers 2

6

Use the Ruby Array (Enumerable) include? method:

array = %w[foo bar ding dong up down bing]
partial_update = true if array.include?(session[:intended_params][:controller])
Sign up to request clarification or add additional context in comments.

2 Comments

not exactly equivalent to what OP expected as it will assign false if none match (not saying original behavior is right)
@VictorMoroz: ah, true, i'll modify slightly.
1
case session[:intended_params][:controller]
when *%w[foo bar ding dong up down bing]
  partial_update =  true
end

might be abusing case statement though

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.