thisisqubika/ruy

When context is not a hash how should ruy respond?

Closed this issue · 2 comments

When setting a condition over a key and not giving a Hash as context, how should ruy respond to it?

Should evaluate it as false or should raise an exception, notifying the user that the given context is not a Hash.

migl commented

I'd say there's no reason to fail when a condition is set over a key but the context is, say, a Fixnum. It'd be perfectly reasonable to silently fail in those cases, but that is just my opinion. In the current version, it'll raise an error when trying to resolve a key on a Fixnum or other kind of object.

If we wanted to raise an error, that is only one case to fail: when you set a condition over a key but pass a context that is not a Hash. I added an error class at this PR: #57

I'd like to discuss this with @snmgian before changing the behavior in a next PR.

If we choose to verify context preconditions, I'd favour duck typing instead of verifying that context is_a?(Hash). If the context has a [] method receiving one argument, Ruy will be happy with that.

For example, context could be an array like ['hello', 'world'], and the rule could have these conditions:

  eq 'hello', 0

I like that kind of flexibility at the cost of not failing quickly when the user makes some types of mistakes.

When something 'fails silently' it implies that the expected action was not performed, like failing to write a file and hidding the error. Ruy rules are like pure functions, without side effects. Conditions evaluate to either true or false, so there's no way to distinguish between a silent failure and a evaluation that turned to be false.

I disagree with conditions silently evaluating to false, because the mirror condition (greater_or_equal_than is the mirror of less_than), within the same context, will also evaluate to false, which is illogical.

I think is preferrable to raise detailed exceptions so users can find the problem quickly.