Skip to content

Method Return Values

CodeMeister edited this page Feb 28, 2023 · 1 revision

predicate methods:

Predicate methods, those that end with a question mark, will always return true or false. They will never return any other value or raise any exceptions.

post.draft?     #=> true
post.archived?  #=> false

getter methods:

Getter methods, those that expect a value to be returned, will always return a 'truthy' value such as a String, Symbol, Array or Hash, as specified in the documentation above. They will never return nil, false nor raise any exceptions.

Post.status_transitions  #=> { draft:     [:archived, :pending],
                         #=>   pending:   [:draft, :published],
                         #=>   published: [:pending, :archived],
                         #=>   archived:  [:published, :draft] }

setter methods:

Setter methods, those that make changes, will always return the expected ActiveRecord response if successfull. If the new state is invalid or the transition is not allowed, an ArgumentError exception will be raised.

Note: the exception is raised even if the change is not being persisted to the database.

post.state = :dummy                 #=> <ArgumentError>
post.update(state: :dummy)          #=> <ArgumentError>
post.update_column :state, :dummy   #=> <ArgumentError>