Coder Social home page Coder Social logo

Comments (10)

mitio avatar mitio commented on August 24, 2024 1

@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: 472
  • grep -rI '%(' . | wc -l: 1046
  • grep -rI '%\[' . | wc -l: 2

Rails master stats for %w{ vs %w( vs %w[:

  • grep -rI '%w{' . | wc -l: 43
  • grep -rI '%w(' . | wc -l: 556
  • grep -rI '%w\[' . | wc -l: 24

from ruby-style-guide.

jibiel avatar jibiel commented on August 24, 2024 1

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.

DAddYE avatar DAddYE commented on August 24, 2024

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.

mitio avatar mitio commented on August 24, 2024

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.

DAddYE avatar DAddYE commented on August 24, 2024

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.

burke avatar burke commented on August 24, 2024

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.

burke avatar burke commented on August 24, 2024

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.

bbatsov avatar bbatsov commented on August 24, 2024

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.

mitio avatar mitio commented on August 24, 2024

@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.

burke avatar burke commented on August 24, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.