Rahul Dess
Rahul Dess

Reputation: 2597

Usage of semi colon for string matching in Ruby?

I am trying to understand if we can use ; inside string match ? Even though the below code doesn't seem to work ..but i am trying to understand what previous developer wanted to do.

if param.type == 'Team'
  search_url += '/type-Team'
elsif param.type == 'Agent;Team;Office'
  search_url += '/type-Agent;Team;Office'
end

Upvotes: 1

Views: 91

Answers (1)

tadman
tadman

Reputation: 211720

Usually untangling some code like this requires re-writing it in a simpler form:

case (param.type)
when 'Team'
  search.url += '/type-Team'
when 'Agent', 'Office'
  search.url += '/type-Agent;Team;Office'
end

If that url part is simpler and this is really just a white-list filter:

case (param.type)
when 'Team', 'Agent', 'Office'
  search.url += "/type-#{param.type}"
end

You'll have to investigate what sort of values you're receiving as type and what should actually be added to the url regardless of what's expressed there.

Upvotes: 1

Related Questions