Walrus the Cat
Walrus the Cat

Reputation: 2404

why does this variable in constants.rb need to be declared to be global?

I want a list to be available to the whole application, so I put it in constants.rb:

hebrew_consonants = ["א", "ב", "ג", "ד", "ה", "ו", "ז", "ח", "ט", "י", "ך", "כ", "ל","ם","מ","ן", "נ", "ס", "ע", "ף", "פ", "ץ", "צ", "ק", "ר", "ש","ת"]

In application_controller.rb, I use the list as follows:

def is_hebrew?(query)
    (0...query.length).each do |index|
        return true if hebrew_consonants.include?(query[index])
    end
    return false
end

I restarted the server, and the app complained that it didn't know about the list. I put dollar signs before its declaration and its reference ($hebrew_consonants), and it worked. With my other constants, they just work. I guess I thought they were supposed to be global variables just by virtue of being in that class. Why is this one behaving differently?

Thank you

Upvotes: 0

Views: 810

Answers (1)

Dty
Dty

Reputation: 12273

First make sure your constants.rb file is in /config/initializers/. Second, try using all caps for hebrew_constants. From the comments in this SO question it sounds like the all caps is necessary.

Upvotes: 1

Related Questions