Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a ruby function to do !(val).nil??

Tags:

ruby

I frequently find myself doing this:

!(val).nil?

My code would be prettier if there was a method like

val.exists?

Is there something like that?

like image 211
djacobs7 Avatar asked Nov 30 '22 11:11

djacobs7


1 Answers

you can use the unless statement, eg,

do_something unless val.nil?

which is probably pretty close to the ideal way to phrase it: if you had a non_nil? method you'd have a negated statement instead of a positive statement like this one.

like image 114
Peter Avatar answered Dec 03 '22 00:12

Peter