0

Now there are a lot of similar questions but no one where the warning occurs in a "case when expression"

I get the warning: already initialized constant for each of my "cases" in a "switch"-statement (I know that they are called different in ruby "switch" = "case" | "case" = "when", but i think you get the idea)

My Code:

Type = case entity.class.to_s
when 'A' then 1
when 'B' then 10
when 'C' then 2
when 'D' then 7
else raise "Unknown Type"
end

How do i get rid of the warning, because i just initialize it at the top, or am i missing something?

Edited: My Code is inside a loop. Like this:

loopcounter = 0
EntityType = 'test'
while loopcounter < 4
    EntityType = case 'StDm'
    when 'StDm' then 1
    when 'Drt' then 10
    when 'Pst' then 2
    when 'StSnet' then 7
    else raise "Unknown EntityType"
    end
    loopcounter += 1
end
puts EntityType
  • 3
    After the _case_ statement, you are trying to reassign the constant `Type` with some value. From that place you were getting _warnings_./ – Arup Rakshit Nov 18 '14 at 10:35
  • See this to stop warnings http://stackoverflow.com/questions/17447532/what-is-the-use-of-usr-local-bin-ruby-w-at-the-start-of-a-ruby-program – Arup Rakshit Nov 18 '14 at 10:39
  • There is no way that you can get that warning from the code you posted. It must originate from some code you didn't post, or from some sort of interaction with some code you didn't post, therefore we can't help you. – Jörg W Mittag Nov 18 '14 at 10:49
  • Have a look at this [discussion](https://www.ruby-forum.com/topic/96121#199230) too... – Arup Rakshit Nov 18 '14 at 11:35
  • Yes i realized, that i forgot to say, that i have this part of the code in a ...each do loop. This was my problem. – user1916608 Nov 18 '14 at 12:45
  • Not sure what you expect your loop to accomplish, because it isn't doing anything different after the first pass. – Mark Thomas Nov 19 '14 at 13:03

1 Answers1

0

I'm so stupid.

Because of the capital letter at the beginning of my variable, ruby thinks its a constant. of course... ARGH...

Well now it is clear to me, why the error occured.