text stringlengths 0 444 |
|---|
def some_other_method |
# body omitted |
end |
end |
---- |
=== `module_function` [[module-function]] |
Prefer the use of `module_function` over `extend self` when you want to turn a module's instance methods into class methods. |
[source,ruby] |
---- |
# bad |
module Utilities |
extend self |
def parse_something(string) |
# do stuff here |
end |
def other_utility_method(number, string) |
# do some more stuff |
end |
end |
# good |
module Utilities |
module_function |
def parse_something(string) |
# do stuff here |
end |
def other_utility_method(number, string) |
# do some more stuff |
end |
end |
---- |
=== Liskov [[liskov]] |
When designing class hierarchies make sure that they conform to the https://en.wikipedia.org/wiki/Liskov_substitution_principle[Liskov Substitution Principle]. |
=== SOLID design [[solid-design]] |
Try to make your classes as https://en.wikipedia.org/wiki/SOLID[SOLID] as possible. |
=== Define `to_s` [[define-to-s]] |
Always supply a proper `to_s` method for classes that represent domain objects. |
[source,ruby] |
---- |
class Person |
attr_reader :first_name, :last_name |
def initialize(first_name, last_name) |
@first_name = first_name |
@last_name = last_name |
end |
def to_s |
"#{first_name} #{last_name}" |
end |
end |
---- |
=== `attr` Family [[attr_family]] |
Use the `attr` family of functions to define trivial accessors or mutators. |
[source,ruby] |
---- |
# bad |
class Person |
def initialize(first_name, last_name) |
@first_name = first_name |
@last_name = last_name |
end |
def first_name |
@first_name |
end |
def last_name |
@last_name |
end |
end |
# good |
class Person |
attr_reader :first_name, :last_name |
def initialize(first_name, last_name) |
@first_name = first_name |
@last_name = last_name |
end |
end |
---- |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.