text
stringlengths 0
444
|
|---|
----
|
# bad
|
while true
|
do_something
|
end
|
until false
|
do_something
|
end
|
# good
|
loop do
|
do_something
|
end
|
----
|
=== `loop` with `break` [[loop-with-break]]
|
Use `Kernel#loop` with `break` rather than `begin/end/until` or `begin/end/while` for post-loop tests.
|
[source,ruby]
|
----
|
# bad
|
begin
|
puts val
|
val += 1
|
end while val < 0
|
# good
|
loop do
|
puts val
|
val += 1
|
break unless val < 0
|
end
|
----
|
=== Explicit `return` [[no-explicit-return]]
|
Avoid `return` where not required for flow of control.
|
[source,ruby]
|
----
|
# bad
|
def some_method(some_arr)
|
return some_arr.size
|
end
|
# good
|
def some_method(some_arr)
|
some_arr.size
|
end
|
----
|
=== Explicit `self` [[no-self-unless-required]]
|
Avoid `self` where not required.
|
(It is only required when calling a `self` write accessor, methods named after reserved words, or overloadable operators.)
|
[source,ruby]
|
----
|
# bad
|
def ready?
|
if self.last_reviewed_at > self.last_updated_at
|
self.worker.update(self.content, self.options)
|
self.status = :in_progress
|
end
|
self.status == :verified
|
end
|
# good
|
def ready?
|
if last_reviewed_at > last_updated_at
|
worker.update(content, options)
|
self.status = :in_progress
|
end
|
status == :verified
|
end
|
----
|
=== Shadowing Methods [[no-shadowing]]
|
As a corollary, avoid shadowing methods with local variables unless they are both equivalent.
|
[source,ruby]
|
----
|
class Foo
|
attr_accessor :options
|
# ok
|
def initialize(options)
|
self.options = options
|
# both options and self.options are equivalent here
|
end
|
# bad
|
def do_something(options = {})
|
unless options[:when] == :later
|
output(self.options[:message])
|
end
|
end
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.