# How to check if a variable is defined?

**URL:** <https://forum.crystal-lang.org/t/how-to-check-if-a-variable-is-defined/5294>\
**Category:** Help & Support\
**Created:** [January 25, 2023, 3:32am UTC](https://forum.crystal-lang.org/t/how-to-check-if-a-variable-is-defined/5294 "2023-01-25T03:32:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 25, 2023, 3:32am UTC](https://forum.crystal-lang.org/t/how-to-check-if-a-variable-is-defined/5294/1 "2023-01-25T03:32:14Z")

</div>

I’d like to be able to check if an array was defined, at run time, before I start adding to it or decide to run a constructor. Is it possible in Crystal?

I come across [this post](https://forum.crystal-lang.org/t/rfc-undefined-type/2695), but seems that this idea have been abandoned.

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [January 25, 2023, 3:46am UTC](https://forum.crystal-lang.org/t/how-to-check-if-a-variable-is-defined/5294/2 "2023-01-25T03:46:11Z")

</div>

Can you share some example code of what you’re wanting to do? It’s probably enough to just make sure it’s not `nil`, or more ideally make it not possible to be `nil` so you can avoid having to handle checking in the first place.

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 25, 2023, 2:32pm UTC](https://forum.crystal-lang.org/t/how-to-check-if-a-variable-is-defined/5294/3 "2023-01-25T14:32:51Z")

</div>

But filling an array with `nil`s before initialization requires me to know it’s size.

So for an example, if I was to parse some user data, where I have no way to know how big it is or how long am I about to parse it, is it currently possible? Just checking if the value is falsy in that hash bellow gives me a `KeyError`, so I can not even access the array to check it’s value.

```auto
def make_stacks(raw_data)
  stacks = Hash(Int32, Array(Char)).new
  char = raw_data.each_char
  (0...raw_data.size).each_slice(4) do |slice|
    item = char.next
    next if item == ' '
    id = (x = ((((slice[-1] + 1) / 4)).to_i % 9)) == 0 ? 9 : x
    if !stacks[id]
      stacks[id] = [] of Char
    end
    stacks[id].push item.as(Char)
  end
  stacks
end

```

---

<div class="post-metadata">

**Author:** ![siery](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/siery/32/1875_2.png) [@siery](https://forum.crystal-lang.org/u/siery)\
**Post date:** [January 25, 2023, 2:36pm UTC](https://forum.crystal-lang.org/t/how-to-check-if-a-variable-is-defined/5294/4 "2023-01-25T14:36:05Z")

</div>

Ok… wait, should I just be checking for exception?

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [January 25, 2023, 2:39pm UTC](https://forum.crystal-lang.org/t/how-to-check-if-a-variable-is-defined/5294/5 "2023-01-25T14:39:10Z")

</div>

Ah okay, so you have a few options in this case.

1. Use `stacks.has_key? id` which will return `false` if the hash doesn’t have that ID as a key. Which would then allow your logic to create the empty array for the id.
2. Can leverage `||=` to allow inlining it all. E.g. `(stacks[id] ||= [] of Char) << item.as Char`
3. Leverage [Hash(K, V) - Crystal 1.7.2](https://crystal-lang.org/api/Hash.html#new%28initial_capacity%3Dnil%2C%26block%3AHash%28K%2CV%29%2CK-%3EV%29-class-method) to create an empty array for that key if it’s not already defined. E.g.

```auto
stacks = Hash(Int32, Array(Char)).new { |hash, key| hash[key] = [] of Char }
# ...
# Can skip checking if it has the key in the first place.
stacks[id] << item.as Char

```
