Comments (10)
@burke, I would mostly agree with you.
I got some stats from the Rails repo I have locally and you can see the results below. I sure think that the lack of convention for the %
literals is not a healthy thing for larger projects and would vote for the guide to include some hints about their usage. Provided we could settle on something... :)
Rails master stats for %{
vs %(
vs %[
:
grep -rI '%{' . | wc -l
: 472grep -rI '%(' . | wc -l
: 1046grep -rI '%\[' . | wc -l
: 2
Rails master stats for %w{
vs %w(
vs %w[
:
grep -rI '%w{' . | wc -l
: 43grep -rI '%w(' . | wc -l
: 556grep -rI '%w\[' . | wc -l
: 24
from ruby-style-guide.
Sorry for the gravedigging, but what's your opinion on contexts like this one:
add_index :thingies, [:category_id, :property], unique: true
add_index :thingies, %i{category_id property}, unique: true
I think the second example loses in readability and this is obvious exception to the rule, since code like this rarely changes and the provided examples (types
/ states
/ roles
) show only one side of the coin: ease of adding/removing items.
from ruby-style-guide.
I agree with you, especially, what is better
%w(foo bar)
%w{foo bar}
%w[foo bar]
In this example I saw second version, but sometimes also third, when Im working with Enums, I prefer to enclose it into []
ie: %w[one two]
when it returns strings or some thing like that I prefer second one i.e.: %r{my/regex}
from ruby-style-guide.
I guess these should be avoided as much as possible, but when I use them:
%w[foo bar baz]
— here I prefer the[]
brackets to associate the list more closely with an array, what it is a literal form of. And usually I try to avoid this construct.%{complex strings}
— I use this for complex and usually multiline strings because I find it easier to read than to have escaped quotes in the string. However, one should ask himself if this "complex" string really needs to be embedded in the code. It surely smells a bit.
These are probably the only two versions of the %
-notion I've ever used in practice.
I sure prefer the /regex\/pattern/
syntax for defining regular expressions. Then again, I don't recall writing awfully complex regexes and probably I've never felt the need to use %r{regex/pattern}
.
from ruby-style-guide.
100% agree with you, I avoid using of %{complex string}
and instead I use (especially for multiline) this:
usage = <<-PLAIN.gsub(/ {2}/, '')
===== EXAMPLE ======
Here some text
explain cmd usage
====================
PLAIN
puts usage
from ruby-style-guide.
I think in general:
- Use
%w
freely. - Use
%r
for regular expressions matching more than one '/' character. - Avoid
%q
,%Q
,%x
,%s
, and%W
. - Use
{}
for delimiters.
Most code I see uses {}
. I guess the question is whether this guide should be prescriptive or descriptive? []
makes sense for the %w
syntax, but isn't used as often.
EDIT: I could be convinced on the %q
point, but I've never felt a need for it.
from ruby-style-guide.
I just skimmed Rails' use of %()
, and I'm now convinced it's actually a great pattern. See:
table << %(<tr><td class="name">#{CGI.escapeHTML(name.to_s)}</td>)
I feel like if this were more documented and encouraged, it would be a great thing.
How about this:
- Use
%w
freely, with()
delimiters. - Use
%()
for single-line strings requiring both interpolation and embedded double-quotes. For multi-line strings, prefer heredocs. - Use
%r
for regular expressions matching more than one '/' character. Use{}
for delimiters, to avoid having to escape capture groups. - Avoid
%q
,%Q
,%x
,%s
, and%W
.
from ruby-style-guide.
I like it. We just need a couple of examples illustrating good and bad style :-) I sense a pull request coming in ( or should I right some examples myself ) ;-)
from ruby-style-guide.
@burke, note that when using the %
literal with {
, [
or (
, you don't need to escape any {
, [
or (
in your string or regexp, provided they have a matching close symbol.
For example, the following examples work:
# prints out 123
puts $1 if %r(some(\d+)text) =~ "some123text"
# evaluates to "this is a function() in a string"
%(this is a function() in a string)
from ruby-style-guide.
@mitio Oh, that's interesting. I'm putting together a section on percent functions right now; I'll use ()
for %r
too. If anyone wants to change that later, I'm okay with.
from ruby-style-guide.
Related Issues (20)
- Typo in loop with break? HOT 1
- New exception classes HOT 1
- File naming with acronyms HOT 3
- `extend`ing objects at runtime HOT 2
- Usage of do...end block syntax in conditionals HOT 1
- Compact case statements: is there a way to enforce single-line when? HOT 4
- Add a rule for method calls in multiline blocks HOT 3
- Good practice - Heredocs in hash HOT 2
- Whitespace around certain operators HOT 6
- Prefer `grep` and `grep_v` to `select`/`reject` that does a regexp match (Ruby 3+)
- Numbered parameters HOT 2
- Is there a typo in the Suppressing Exceptions section? HOT 2
- Add guidance for safe navigation operator (`&.`) HOT 3
- Why not allowing multi-line ternary operator? HOT 2
- `using` in "Consistent Classes" HOT 6
- The '%w' and '%i' syntax for arrays is just a bad idea HOT 10
- Consider definining `#nil?` method a bad practice HOT 4
- Enforce no parentheses for `include`, `extend`, etc?
- [Idea] Write "good" before "bad" examples HOT 2
- /.,m/.,m/.,m/.,m./,m.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ruby-style-guide.