text
stringlengths
0
444
# good
l = ->(a, b) { a + b }
l.call(1, 2)
l = lambda do |a, b|
tmp = a * 7
tmp * b / 50
end
----
=== Stabby Lambda Definition with Parameters [[stabby-lambda-with-args]]
Don't omit the parameter parentheses when defining a stabby lambda with parameters.
[source,ruby]
----
# bad
l = ->x, y { something(x, y) }
# good
l = ->(x, y) { something(x, y) }
----
=== Stabby Lambda Definition without Parameters [[stabby-lambda-no-args]]
Omit the parameter parentheses when defining a stabby lambda with no parameters.
[source,ruby]
----
# bad
l = ->() { something }
# good
l = -> { something }
----
=== `proc` vs `Proc.new` [[proc]]
Prefer `proc` over `Proc.new`.
[source,ruby]
----
# bad
p = Proc.new { |n| puts n }
# good
p = proc { |n| puts n }
----
=== Proc Call [[proc-call]]
Prefer `proc.call()` over `proc[]` or `proc.()` for both lambdas and procs.
[source,ruby]
----
# bad - looks similar to Enumeration access
l = ->(v) { puts v }
l[1]
# good - most compact form, but might be confusing for newcomers to Ruby
l = ->(v) { puts v }
l.(1)
# good - a bit verbose, but crystal clear
l = ->(v) { puts v }
l.call(1)
----
== Methods
=== Short Methods [[short-methods]]
Avoid methods longer than 10 LOC (lines of code).
Ideally, most methods will be shorter than 5 LOC.
Empty lines do not contribute to the relevant LOC.
=== Top-Level Methods
Avoid top-level method definitions. Organize them in modules, classes or structs instead.
NOTE: It is fine to use top-level method definitions in scripts.
[source,ruby]
----
# bad
def some_method; end
# good
class SomeClass
def some_method; end
end
----
=== No Single-line Methods [[no-single-line-methods]]
Avoid single-line methods.
Although they are somewhat popular in the wild, there are a few peculiarities about their definition syntax that make their use undesirable.
At any rate - there should be no more than one expression in a single-line method.