Just released a gem inspired by NullObject and thoughbot/wrapped, looking for suggestion!

Hey, I just released a gem called Probably, it was inspired by NullObject episode and thoughtbot/wrapped gem. You may check it out here:

With this gem, instead of writing like this

user.most_recent.try(:answer) || Answer::MISSING

you just write

user.probably.most_recent.answer.or(Answer::MISSING)

or like this

probably(user).most_recent.answer.or(Answer::MISSING)

I’m looking for any advice for improvement on my code, and I appreciate any help from you guys!

Hey @Yi_Wei, this looks like a pretty good idea.

A while back, I started work on a NullRecord gem trying to implement an option for ActiveRecord models with an interface along the lines of

with_null_record :name_of_null_object

and providing a initializer to act as a registry for these. I then discovered Advi Grimm’s Naught and that kind of made cool off. I’ll probably give it a go as an exercise when I have some downtime.

I would give you some advice, if that is okay.

First off, look at Advi’s gem for inspiration, it is quite interesting.

Second, I wouldn’t conflate functors/monads and null objects. I would split them in to two gems and make my best to enable their interfaces to be compatible.

Also, sharing a link to a repo will get people looking at your code and helping out :slight_smile: I am aware this is one of those “here is some advice I am not actually taking myself” moments…)

Anyway, hope this helps :slight_smile:

Hey @pedromoreira! Thanks for your advice! I already edited my post to provide a link to my repo, and I just started to look at Advi’s gem, thanks for mentioning it! Really appreciate your help!

No worries, glad to help :slight_smile:

This is cool, and I think it’s great to experiment with alternatives to nil in Ruby.

One thing I’d keep in mind is that you want to isolate nil-checking as much as possible. For example, in this example:

user.probably.most_recent.answer.or(Answer::MISSING)

It may be beset to return a probably instance in user:

def user
  super.probably
end

def most_recent_answer_text
  user.most_recent.answer.or(Answer::MISSING)
end

This way, you can’t ever forgot to call probably before attempting to invoke methods on user.

Great idea, thanks!