text
stringlengths 0
444
|
|---|
# good
|
date = `date`
|
echo = %x(echo `date`)
|
----
|
=== `%s` [[percent-s]]
|
Avoid the use of `%s`.
|
It seems that the community has decided `:"some string"` is the preferred way to create a symbol with spaces in it.
|
=== Percent Literal Braces [[percent-literal-braces]]
|
Use the braces that are the most appropriate for the various kinds of percent literals.
|
* `()` for string literals (`%q`, `%Q`).
|
* `[]` for array literals (`%w`, `%i`, `%W`, `%I`) as it is aligned with the standard array literals.
|
* `{}` for regexp literals (`%r`) since parentheses often appear inside regular expressions. That's why a less common character with `{` is usually the best delimiter for `%r` literals.
|
* `()` for all other literals (e.g. `%s`, `%x`)
|
[source,ruby]
|
----
|
# bad
|
%q{"Test's king!", John said.}
|
# good
|
%q("Test's king!", John said.)
|
# bad
|
%w(one two three)
|
%i(one two three)
|
# good
|
%w[one two three]
|
%i[one two three]
|
# bad
|
%r((\w+)-(\d+))
|
%r{\w{1,2}\d{2,5}}
|
# good
|
%r{(\w+)-(\d+)}
|
%r|\w{1,2}\d{2,5}|
|
----
|
== Metaprogramming
|
=== No Needless Metaprogramming [[no-needless-metaprogramming]]
|
Avoid needless metaprogramming.
|
=== No Monkey Patching [[no-monkey-patching]]
|
Do not mess around in core classes when writing libraries (do not monkey-patch them).
|
=== Block `class_eval` [[block-class-eval]]
|
The block form of `class_eval` is preferable to the string-interpolated form.
|
==== Supply Location [[class-eval-supply-location]]
|
When you use the string-interpolated form, always supply `+__FILE__+` and `+__LINE__+`, so that your backtraces make sense:
|
[source,ruby]
|
----
|
class_eval 'def use_relative_model_naming?; true; end', __FILE__, __LINE__
|
----
|
==== `define_method` [[class-eval-define_method]]
|
`define_method` is preferable to `class_eval { def ... }`
|
=== `eval` Comment Docs [[eval-comment-docs]]
|
When using `class_eval` (or other `eval`) with string interpolation, add a comment block showing its appearance if interpolated (a practice used in Rails code):
|
[source,ruby]
|
----
|
# from activesupport/lib/active_support/core_ext/string/output_safety.rb
|
UNSAFE_STRING_METHODS.each do |unsafe_method|
|
if 'String'.respond_to?(unsafe_method)
|
class_eval <<-EOT, __FILE__, __LINE__ + 1
|
def #{unsafe_method}(*params, &block) # def capitalize(*params, &block)
|
to_str.#{unsafe_method}(*params, &block) # to_str.capitalize(*params, &block)
|
end # end
|
def #{unsafe_method}!(*params) # def capitalize!(*params)
|
@dirty = true # @dirty = true
|
super # super
|
end # end
|
EOT
|
end
|
end
|
----
|
=== No `method_missing` [[no-method-missing]]
|
Avoid using `method_missing` for metaprogramming because backtraces become messy, the behavior is not listed in `#methods`, and misspelled method calls might silently work, e.g. `nukes.luanch_state = false`.
|
Consider using delegation, proxy, or `define_method` instead.
|
If you must use `method_missing`:
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.