The Crystal Programming Language Forum

Is there something like `presence` in Ruby?

I’d like to return a string only if not nil and not blank (else return nil). Is there anything like that baked into the language or the standard library?

a = nil
a.presence # nil
b = ""
b.presence # nil
c = "abc"
c.presence # "abc"

https://crystal-lang.org/api/master/String.html#presence:self?-instance-method

It’s currently only in master so will have to wait for the next release.

4 Likes

Thanks! So looking forward to the release.

Could also use a nightly release via Docker, Snap, or build from source until then.