Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

or_else method #9

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

or_else method #9

wants to merge 2 commits into from

Conversation

rap1ds
Copy link
Owner

@rap1ds rap1ds commented Aug 23, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant