Github Ruby Styleguide
11–20 of 73 posts
Re: Github Ruby Styleguide
#12I'm a little curious why %w(strings go here) is preferred to [strings, go, here] The first form requires extra work to escape spaces, the second form is a representation of an array everywhere in Ruby, including in the debugger and IRB.
Re: Github Ruby Styleguide
#13Re: Github Ruby Styleguide
#14I can't stress enough how important it is to _not_ use `and` and `or`. As a former die-hard fan of those operators, it bit me in the ass so hard when those didn't exactly work the way they were supposed to. I've been using && and || now for years and I haven't looked back. /PSA
[0] http://stackoverflow.com/questions/1426826/difference-betwee...
Re: Github Ruby Styleguide
#15I agree with almost everything except for using named groups over the $1-$9 variables in regular expression matching. This may be the case if there are many capture groups in the expression, but I would argue that /(? regexp)/ =~ string is a fair deal more difficult to read than simply /(regexp)/ =~ string Almost everybody familiar with PCRE will be familiar with the simpler form, and it's usually the case that the s…
Re: Github Ruby Styleguide
#16I'm a little curious why %w(strings go here) is preferred to [strings, go, here] The first form requires extra work to escape spaces, the second form is a representation of an array everywhere in Ruby, including in the debugger and IRB.
The first form is generally used for symbol-type strings (word lists, essentially, hence "w"). It is less syntactically noisy, shorter and requires less waffling around with non-word keys.
That's especially flagrant if you don't ignore half the actual array,
%w(foo bar baz)
really results in ["foo", "bar", "baz"]
(the quotes are rather important, because they're noisy, repetitive and annoying to type).It's one of the few things I regularly wish were in more languages.
Re: Github Ruby Styleguide
#17I'm not a Ruby programmer, but a couple of these things just look completely wrong: # good def some_method(some_arr) some_arr.size end Omitting the return statement is a good thing? How do you know if this method even returns anything? .size could be a method that doesn't return anything, right, since parens are optional? #good do_something if something_else This, to me, is horrible. If you're just scanning down the…
It can only be a method. Instance attributes can not be public in ruby (and are delineated by the `@` prefix sigil), the closest equivalent (attr_reader/attr_writer/attr_accessor) is a dynamically generated method (or pair of methods)
Re: Github Ruby Styleguide
#18I'm not a Ruby programmer, but a couple of these things just look completely wrong: # good def some_method(some_arr) some_arr.size end Omitting the return statement is a good thing? How do you know if this method even returns anything? .size could be a method that doesn't return anything, right, since parens are optional? #good do_something if something_else This, to me, is horrible. If you're just scanning down the…
and (without any form of negative judgement), this is why
> a couple of these things just look completely wrong
to you, because they're idiomatic of Ruby.
First, lack of return.
A random example in Rails's ActiveSupport[0]:
def to_s
"(GMT#{formatted_offset}) #{name}"
end
It just reads like ":to_s is defined as being a String constructed this way". Note that formatted_offset and name are resolved as methods in the context (i.e self), and self. is simply omitted, which is also idiomatic.Not only functions always return something (their last statement's return value), every statement actually returns something, so
foo = if a == b then 1 else 2
works and is used, but also with case/when, and whatever you can think of. With proper indentation this is perfectly sane and readable, and avoid redundant, 'side effect' (from the point of view of code flow) assignments, easing comprehension and refactoring, while making mistakes less likely (foo will always and obviously be assigned something).What's more, a Ruby construct known as blocks are used with methods (such as Enumerable#map or Enumerable#select) needing return values, and omitting return is a readability and typing boon.
That's not to say return has no use, but then when you use it, you mean it, so that it is used in match/exit pattern methods, where you're doing some checks, and dispatch-return according to various situations, i.e you return early.
It is also used in Procs, which differ from lambdas in that a return will return from the caller, not the Proc.
Second, right-side if.
This aids readability when you're concerned about doing a number of sequential tasks, and some of them are conditioned. All possible tasks are aligned, and all conditions are on the right side. An absence of condition means "always". Sounds almost like column-based source code (fortran, RPG on AS400...).
You mention scanning down the file, but idiomatic ruby would not make you scan down the file: you would be faced with a function, whose size would make its structure/pattern apparent, and with such a pattern the logic would be readily seen, dangling on the right side.
Such conditions are often used with the first part of this discussion, so that you see immediately what is returned right there on the left, possibly conditioned.
[0]: https://github.com/rails/rails/blob/5fe88b11f11bb3b30bc23c57...
Re: Github Ruby Styleguide
#19I can't stress enough how important it is to _not_ use `and` and `or`. As a former die-hard fan of those operators, it bit me in the ass so hard when those didn't exactly work the way they were supposed to. I've been using && and || now for years and I haven't looked back. /PSA
To anyone who's wondering, it seems like 'and' and 'or' are the same as && and ||, but with lower precedence [0]. Does anyone have a good reason why they both exist? It's a pretty big gotcha. [0] http://stackoverflow.com/questions/1426826/difference-betwee...
If `foo == bar` evaluates to true, the `or` is short-circuited, else it calls the built-in `die` function which kills the process.
Re: Github Ruby Styleguide
#20 # good
names.select { |name| name.start_with?("S") }.map { |name| name.upcase }
I've noticed that sort of chaining is changing towards: names.select { |name| name.start_with?("S") }
.map { |name| name.upcase }
Which can be further chained (if need be) like so: names.select { |name| name.start_with?("S") }
.map { |name| name.upcase }
.sort
.join(', ')
Which, imo, is just better than trying to do it all on one line or using intermediate variables. It's more of a value call when it comes to the intermediate variables, though. I'd also use .map(&:upcase)
instead of .map { |word| word.upcase }
But I think it's perfectly understandable if you find the word.upcase more explicit and readable.