Perry Horwich
Perry Horwich

Reputation: 2846

How can I ensure a reserved word will not conflict with Ruby, Rails 4, or a gem in my gemlist

I have a Rails4 application to which I want to add a model named 'Parameters' It's a good choice in terms of the models purpose, but I worry that it is also a common programming term.

What is the best practice (individual steps) to ensure that:

Parameters  
parameter.thing

... will not conflict with a reserved word in Ruby, Rails, or my particular Gem list?

Upvotes: 1

Views: 208

Answers (2)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

Ruby has a concept of open classes. That said, when the class MyClass is defined somewhere, it’s perfectly valid to have the following in your code:

class MyClass
  def another_method
    # do something useful
  end
end

That said, there is no “reflection-like” mechanism for this action, since it is how Ruby is intended to work.

One might build the complicated logic like this:

  • run the application without new Parameters class loaded;
  • call $defined = const_get('Parameters') || true rescue false;
  • use $defined global (it’s made global here for the sake of an example) to determine whether the name is free.

or:

or

or even

  • parse sources.

But I would not go with any of solutions above. Just call your class MyAppParameters and live with this name.

Upvotes: 1

MarsAtomic
MarsAtomic

Reputation: 10696

An IDE like RubyMine should warn you by coloring the text whenever you use a reserved word, but for people like me who use glorified text editors, you can always check this list of Rails related reserved words when you're not sure.

Upvotes: 1

Related Questions