---
title: "Tiny Tip: Inline if-else statements"
author: "Yaroslav Shmarov"
date: "2021-10-18"
canonical_url: "https://blog.superails.com/inline-if-else-statements"
markdown_url: "https://blog.superails.com/inline-if-else-statements.md"
description: "a) This can be written like this b) This can be written like this"
tags: ["ruby","rails","ruby-on-rails","if-else","tiny-tip"]
license: "GNU AGPL v3. Credit Yaroslav Shmarov and link the canonical URL."
---

# Tiny Tip: Inline if-else statements

a) This
```ruby
  if post.published?
    'published'
  else
    'draft'
  end
```
can be written like this
```ruby
  post.published? 'published' : 'draft'
```

b) This
```ruby
if post.published?
  'published'
elsif post.draft?
  'draft'
else
  'archived'
end
```
can be written like this
```ruby
  post.published? ? 'published' : post.draft? 'draft' : 'archived'
```
