4 ms·
This may be the clearest explanation of Ruby syntax I've ever read! My attempt to parse the above example comes to something like this: state_machine(STAT
by csense 13y ago
This may be the clearest explanation of Ruby syntax I've ever read!
My attempt to parse the above example comes to something like this:
state_machine(STATE, {"initial" : ACTIVE}, function()
{
after_transition({"any" : BLOCKED}, function(user, transition)
{
user.users_projects.find_each(function(membership)
{
return (membership.destroy ? true : false);
});
});
});
I'm writing in JavaScript because its function notation is way better than Python's lambda syntax. From the parent's explanation, I'm pretty sure this is a valid translation, even though I don't know Ruby. But having three levels of nested anonymous functions really makes the code hard to understand. I'm guessing that this is idiomatic Ruby though, and if you program with the pattern long enough, it gets easier.
OTOH it's still a barrier to entry entirely separate from the syntax -- if the semantics of Ruby code you see "in the wild" is typically this complicated, it's almost as bad as Haskell and its monads! (I have much stronger math chops than most programmers, I've tried to read introductions to monads twice, had them explained to me three separate times on HN, and still don't understand them at all!)
- relix 13y agoYep, just about, although if you look closely, the `any` in the original code is not a symbol, but is actually a variable or a method (could be either, we can't know from the sample). Also, the return in that block would cause an exception, because it doesn't do what the original programmer thinks it does. I guess he never tried out what happens if destroy returns false (which would happen if destroy fails, e.g. because a validation failed).