Perry Horwich Perry Horwich - 4 months ago 13
Ruby Question

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?

Answer

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.