O
O
Ord EO2020-04-23 15:12:40
ruby
Ord EO, 2020-04-23 15:12:40

How to replace a word with another in a Ruby variable?

Some variable = race comes to me.
In which there can be a number of values, example ("Black", "AI / AN**", "NH/PI**", White, Asians)

How can I make my variable automatically change the value according to this algorithm, if in it
Black should become African American,
White - white,
AI / AN ** - American Indian or Alaskan Native
NH / PI - Native Hawaiian or Other Pacific Islander
I tried through gsub - but it does not change the variable itself
How can this be implemented by ruby or regular expressions?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Tigran Abrahamyan, 2020-04-23
@OrdeO

words = ["Black", "AI/AN**", "NH/PI**", "White", "Asians"]

words.each do |word|
  case word
  when "Black"
    word.gsub!("Black", "African American")
  when "AI/AN**"
    word.gsub!("AI/AN**", "American Indian or Alaskan Native")
  when "NH/PI**"
    word.gsub!("NH/P**", "ative Hawaiian or Other Pacific Islander")
  end
end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question