programming • It’s good for Domain-Specific Languages • It allows to write prototypes very fast • It has a huge ecosystem with tons of gems and development tools.
means that you need to do all type checks manually. x = 5 y = “string" lambda = ->{ x + y } # you will not get an # error until execution # next code line: lambda.call
# NoMethodError: undefined # method `+' for nil n = 1 #=> 1 n = 2 && n + 1 #=> 2 n = 2 && n + 1 #=> 3 n = 2 && n + 1 #=> 4 n = 2 && n + 1 #=> 5 n #=> 5 n = 1 and n + 1 #=> 2 n = 1 #=> 1 n = 2 and n + 1 #=> 3 n = 2 and n + 1 #=> 3 n = 2 and n + 1 #=> 3 n = 2 and n + 1 #=> 3 n #=> 2
superclass to get a superset of all records in many cases. class Article < Publication has_many :comments # You can’t do this: Publication.order(published_at: :desc) .includes(:comments) .where(comments: {approved: true})
validation and constraints (uniqueness and NOT NULL, for example). • We need an additional “type” column for metadata, but AR models don’t protect it as a metadata-column. • Totally misused cases of STI, like states, statuses, roles (something changeable). class ProcessedOrder < Order end class Admin < User end
Article (Aggregate) is a composition over Publication and other entities. • ProcessedOrder shouldn’t be inherited from Order, but Order (Aggregate) is a composition over OrderItems, Delivery, Payment, etc. • Admin shouldn’t be inherited from User, but Admin (Aggregate) is a composition over User and other related entities.
tests for models, because model has a lot of responsibilities. Here mock. There stub. There one more mock. Mock, mock, mock, … Stub. validate :title, presence: true has_many :comments, dependent def add_comment(attrs) comments.create attrs end before :create, :assign_slug
pseudo-keys instead of natural keys. In most cases you have no ID or anything of the kind. Your records should be identified with a natural key even if it could be composite.