# Rename \`Object#try(&block)\` to \`Object#not\_nil?(&block)\`

**URL:** <https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290>\
**Category:** Crystal Contrib\
**Created:** [June 29, 2020, 1:49pm UTC](https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290 "2020-06-29T13:49:38Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![akadusei](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/akadusei/32/467_2.png) [@akadusei](https://forum.crystal-lang.org/u/akadusei)\
**Post date:** [June 29, 2020, 1:49pm UTC](https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290/1 "2020-06-29T13:49:38Z")

</div>

`#try(&block)`'s use case, currently, is _to do something only if the object is **not nil** _. To that end, `#not_nil?(&block)` seems to me to be more a more intuitive name.

We already have `#not_nil!` and `#nil?`. So we could add `#not_nil?` which would be the inverse of `#nil?`, and `#not_nil?(&block)` as a replacement for `#try(&block)`:

```crystal
class Object
  # ...
  def not_nil?
    !nil?
  end
  # ...
  def not_nil?
    yield self
  end
  # ...
end

struct Nil
  # ...
  def not_nil?(&block)
    self
  end
  # ...
end

```

Maybe it’s just me, but in the face of the _nils_ and _not nils_, _try_ seems to be the odd one out.

Of course, this means breaking changes and more work; I don’t know if it’s worth it?

Related?: [#6600](https://github.com/crystal-lang/crystal/issues/6600)

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [June 29, 2020, 2:01pm UTC](https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290/2 "2020-06-29T14:01:51Z")

</div>

I don’t think a rename is worth it at this point. This will cause a massive breaking change for no good reason.

By the way, we copied the `try` name from ActiveSupport: [https://guides.rubyonrails.org/active\_support\_core\_extensions.html#try](https://guides.rubyonrails.org/active_support_core_extensions.html#try)

---

<div class="post-metadata">

**Author:** ![RespiteSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/respitesage/32/793_2.png) [@RespiteSage](https://forum.crystal-lang.org/u/RespiteSage)\
**Post date:** [June 29, 2020, 2:37pm UTC](https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290/3 "2020-06-29T14:37:48Z")

</div>

Also, `Object#not_nil?(&block)` looks like a method that returns a boolean, not a method that returns the result of the block (or `nil`). For example, `1.try { |n| n + 2 }` returns `3`, not `true` or `1` (to return `1`, you’d use `Object#tap`).

---

<div class="post-metadata">

**Author:** ![andraantariksa](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/andraantariksa/32/629_2.png) [@andraantariksa](https://forum.crystal-lang.org/u/andraantariksa)\
**Post date:** [June 29, 2020, 3:07pm UTC](https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290/4 "2020-06-29T15:07:11Z")

</div>

`Object#not_nil?` is like asking the `Object` “is it not nil?” which should return a boolean type.

---

<div class="post-metadata">

**Author:** ![akadusei](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/akadusei/32/467_2.png) [@akadusei](https://forum.crystal-lang.org/u/akadusei)\
**Post date:** [June 29, 2020, 5:29pm UTC](https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290/5 "2020-06-29T17:29:35Z")

</div>

Cc: @andraantariksa

That’s not definite. There are methods in the standard library with a `?` suffix that return a nilable value, not a boolean. A few examples:

- `Enumerable#[]?`
- `Enumerable#max?`
- `Enumerable#min?`

The general conventions seems to be that a `?` suffix is used for methods that return a boolean, but also for ones that return a nilable object.

---

<div class="post-metadata">

**Author:** ![akadusei](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/akadusei/32/467_2.png) [@akadusei](https://forum.crystal-lang.org/u/akadusei)\
**Post date:** [June 29, 2020, 5:32pm UTC](https://forum.crystal-lang.org/t/rename-object-try-block-to-object-not-nil-block/2290/6 "2020-06-29T17:32:36Z")

</div>

That’s understandable. However, this came to mind whenever I’ve had to call a `#try`, so I thought I should bring it up.
