Tips & Gotchas
In a few short pages we have covered a lot of ground, but really we have only scratched the surface of what Ruby2JS can do. These pages make it look easy, but there are a few things to watch out for.
Ruby2JS solves this by detecting the use of parenthesis to distinguish between these two cases. First, the rules of thumb, and then some of the significant exceptions:
- Always use parenthesis on method definitions when there are zero arguments.
- Always use parenthesis on method calls when there are zero arguments.
- Never use parenthesis on definitions of attribute reader / property getters.
If you follow these rules, you will never have a problem. All but the last example (Content Loader) followed these rules.
selfwithin the definition of classes, the type of which is always very much known.
If you are careful on your method and accesssor definitions, then references to these methods and attributes within the class can be determined at compile time, enabling the omission of parenthesis in intra-class calls even when there are no arguments.
It just so happens that Stimulus hits this sweet spot as every definition is a class.
In Ruby, every statement is an expression and returns a value. The
This is not much of a problem for Stimulus as neither lifecycle methods nor actions are expected to return a result.
One solution that may be a bit of overkill: the return filter that adds a return statement to every method defintion. This generally is harmless, but may make the generated code marginally bigger and marginally less readable.
Got questions or comments? Join the community!