How to check if an element exists and append string

I have few items in my_list:

@my_list = {
  "all": [
    "prd02",
    "stg01",
    "prd01",
    "stg02"
    ],
  "prd": [
    "prd02",
    "prd01"
    ],      
  "stg": [
    "stg02",
    "stg01"
    ] 
}

Currently, I print all of them in all:

if @my_list['all'].include?(@item)
puts "#{@item}"

My current output is:

 prd02
 stg01
 prd01
 stg02

How can I check if that item is present in "prd" before printing the items? It should append “(prod)” and if that item is in "stg" it should append “(staging)”.

I want to get my desired output as:

prd02 (prod)
stg01 (staging)
prd01 (prod)
stg02 (staging)

How can I check if the item is present in prd or stg?

if @my_list['all'].exists ? @my_list['prod'](@item)
puts "#{@item}.(prod)"

Answer

This should work:

my_list = {
  "all": [
    "prd02",
    "stg01",
    "prd01",
    "stg02"
    ],
  "prd": [
    "prd02",
    "prd01"
    ],
  "stg": [
    "stg02",
    "stg01"
    ]
}

my_list[:all].each do |item|
  if my_list[:prd].include?(item)
    puts "#{item} (prod)"
  elsif my_list[:stg].include?(item)
    puts "#{item} (staging)"
  end
end

Please also note:

  1. You have a syntax error in my_list declaration (missing ,)

  2. You need to use :all, :prd and :stg as symbols

See “What’s the difference between a string and a symbol in Ruby?“.