As a not young/not old programmer many of these concepts are really new and it takes time to wrap one’s head around both how to use them and more interestingly why! The whole “dry” naming of this suite of gems has made me come back over and over to see how I can leverage this work.
In my case Javascript promises have have made the monad pattern much more familiar and I am seeing and really liking what I see. In fact I’ve been using my own form of a ‘monad’ for a long time although not nearly as elegant.
Here’s my question … I have a method that does work that will either succeed or fail. I am using the Result monad that allows the method to provide a Success or Failure result. So the calling code, a rails controller in this case, currently looks like:
my_object.hard_work( inputs ).bind do |reward|
render json: reward, status: :ok
end .or do |too_bad|
render json: too_bad, status: :service_unavailable
end
I my question here is the choice of ‘bind’ and ‘or’ as the methods names. I would think this would be more interesting:
my_object.hard_work( inputs ).successful do |reward|
render json: reward, status: :ok
end .failure do |too_bad|
render json: too_bad, status: :service_unavailable
end
I’m sure I’ve missed something here because the first case just doesn’t feel as satisfying as it should.
Could I get a pointer or two? I suspect that there many others asking similar questions … I hope!
Many thanks for all the hard work on dry.rb
P.S. As I think a bit further … is there a form that eliminates the need for block arguments?
my_object.hard_work( inputs ).successful do
render json: successful_value, status: :ok
end .failure do
render json: failure_value, status: :service_unavailable
end
i.e. “successful_value” and “failure_value” are provided to the programmer by default
And then stylistically I might even prefer the following:
my_object.hard_work( inputs ).successful {
render json: successful_value, status: :ok
}.failure {
render json: failure_value, status: :service_unavailable
}
I find these last two forms very satisfying - so how do I get here? This is what I might be missing.