Ruby - Naming Convention - letter case for acronyms in class/module names?
SCM::SVN
looks best to me. Rails is full of classes like ERB, ORM and OMFGIMATEAPOT. And that's not to mention things like JSONSerializer. Ruby's source has a bunch of acronyms, too. The most obvious example to me is YAML
. The standard as I've seen it is to upcase letters for CamelCase but generally not to downcase them (although Rails has opinions on model names).
If you have grep and the source code you can see plenty of examples with something like
grep -r 'class [A-Z]\{3,\}' <path/to/source>
# or, if you only want acronyms and nothing like YAMLColumn:
grep -rw 'class [A-Z]\{3,\}' <path/to/source>
Is it a poor practice to use all caps for class names in Ruby?
According to the Ruby style guide on https://github.com/bbatsov/ruby-style-guide , you should:
Use CamelCase for classes and modules. (Keep acronyms like HTTP, RFC,
XML uppercase.)
The ID part should be all uppercase. But I'm not sure why you want to shorten the Foo. The best solution would be to use something like FooID.
JSON is all caps because it's an abbreviation.
can make module name uppercase?
In Rails you can use inflect.acronym
to setup special inflection rules for acronyms like IPAdress or HTTPClient which don't follow the normal camelcase convention.
# config/initializers/inflections.rb
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'JOB'
end
But this is a pretty bad idea to begin with. job is not an acronym and its just a blatant violation of the principle of least surprise as well as the holy Ruby naming conventions.
Having a module named "Modules" is also just silly. Organize your code around meaningful names that convey its actual purpose.
What does Modules::JOB::Web
do? (retorical question)
Rails custom validator name error
Your validator should be named UrlValidator
(the result of camelized 'url'
):
# Validator for the `url` attribute
class UrlValidator< ActiveModel::EachValidator
def validate_each(record, attribute, value)
# to be implemented
end
end
def Foo < ActiveRecord::Base
validates :url, :presence => true
end
Link to doc
or, to stick with the name URLValidator
, you can use validates_with
:
class URLValidator< ActiveModel::EachValidator
def validate(record)
# check on record.url, to be implemented
end
end
def Foo < ActiveRecord::Base
validates_with URLValidator
end
or a more generic version (if the name of the attribute is not always url
)
class URLValidator< ActiveModel::EachValidator
def validate(record)
value = record.send(options[:on])
# check value, to be implemented
end
end
def Foo < ActiveRecord::Base
validates_with URLValidator, :on => :url
end
Ruby capital letter variables dilemma in algorithmic code
Usually the solution is just to choose much more meaningful names. E.g.
def average(values)
total = 0
values.each { |value| total += value }
total/values.length.to_f
end
Why is F_weight
helpful. F is for Force?
Fine force_weight
, erm no: force_mass
. After all weight is a force.
Now if you were talking about N as in the set of natural numbers, I could see some point if N wasn't a local variable.
Override Rails controller routing with capital letters in model name
I had this issue and after trying all of the above solutions; was able to fix my problem using the inflector.
In my case the issue was that TLA::ThingsController
was being resolved as Tla::ThingsController
putting the following in my initializers folder fixed it
config/initializers/inflections.rb
ActiveSupport::Inflector.inflections do |inflect|
inflect.acronym 'TLA'
end
Routing Error Uninitialized Constant Rails
I ended up just changing the names of my models and regenerating the controllers and views. I think what was throwing me off was I named my models with single letters representing words: s_n_d_subs
which was causing me confusion when I generated controllers and views. So what I did was changed my model names so each part separated by an underline had at least two letters: ex: surv_dev.rb
. This way when I generated controllers and routes it all worked just fine.
Related Topics
Upsert Multiple Records with Mongodb
How to Add Conditional Rubygem Requirements to a Gem Specification
Rails Gem to Break a Paragraph into Series of Sentences
Rails 7 Signup Form Doesn't Show Error Messages
Ruby: Any Gems for Threadpooling
How to Get a Reference to a Method
Ruby on Rails: Converting "Somewordhere" to "Some Word Here"
Why Does Date.Yesterday Counts as Date.Today Also
Https Request Using Net::Http's Block Form -- Is It Possible
What Does a Single Splat/Asterisk in a Ruby Argument List Mean
Post Redirect Get Pattern in Rails
Include Erb Delimiters Inside of a String in an Erb Block
Triple Single Quote VS Triple Double Quote in Ruby
How to Run an Excel MACro from Ruby
The Program 'Rails' Is Currently Not Installed
In Rspec, Using Let Variable Inside Before :All Block
Can't Find Gem Railties (>= 0.A) with Executable Rails (Gem::Gemnotfoundexception)
How to Enable Colors with Rspec When Using Jruby or Bundle Exec