ruby-possibly icon indicating copy to clipboard operation
ruby-possibly copied to clipboard

`or_else` method

Open rap1ds opened this issue 11 years ago • 0 comments

or_else

or_else returns the current Maybe if it's a Some, but if it's a None, it returns the parameter that was given to it (which should be a Maybe).

Here's an example: Show "title", which is person's job title or degree if she doesn't have a job or "Unknown" if both are missing.

maybe_person = Maybe(person)

title = maybe_person.job.title.or_else { maybe_person.degree }.get_or_else { "Unknown" }

title = if person && person.job && person.job.title.present?
  person.job.title
elsif person && person.degree.present?
  person.degree
else
  "Unknown"
end

rap1ds avatar Aug 23 '14 21:08 rap1ds