4 ms·
I am often frustrated by this as well, but I'm not sure it's specific to Python or Ruby. Why is it Math.log(1) rather than 1.log()? And given whatever reason i
by equark 16y ago
I am often frustrated by this as well, but I'm not sure it's specific to Python or Ruby. Why is it Math.log(1) rather than 1.log()? And given whatever reason it's Math.log, why is it a.inverse() when a is a Matrix not MatrixMath.inverse(a)?
Is there a good guideline for static method vs. instance method distinction?
- ominous_prime 16y agoIn python the convention tends to be: a.inverse() -> mutates 'a' to its inverse MatrixMath.inverse(a) -> returns the inverse of 'a', leaving 'a' unchanged
- xtho 16y ago1 isn't a matrix. Integers/floating point numbers are in many languages treated differently than other types for reasons of performance and memory management etc. Ruby isn't different in this respect (IIRC they call it immediate objects). A matrix is a "normal" object in ruby. BTW the ruby equivalent for the above convention would be (maybe inspired by scheme?) a.inverse!() -> mutate a a.inverse() -> return the inverse Which one is more consistent?
- Confusion 16y ago1 isn't a matrix A Fixnum could be interpreted as a 1x1 matrix. For some recursive solutions, it would probably be nice of MatrixMath.inverse accepted it as such, as well as [[1]].
- rikthevik 16y agoI am a python user, but I _really_ like that .inverse!() notation indicating that the object itself is being mutated. It's concise and consistent. Sometimes punctuation in names is exactly what you need. Same with ? for predicates. I'll take even? over evenp any day of the week.
- aphyr 16y agoThis confuses me, because python strings use str.lower() to return a lowercase copy of str, leaving str unchanged. I prefer the Ruby idiom of naming the mutating versions with a !.
- nanairo 16y agoI agree with you, and I was equally frustrated. I think the problem is that most people don't use the Math module that much. Hence they try to keep the Numeric classes smaller rather than filling them with all possible Math functions. However, it doesn't take much to "fix" it the way you like it. You need to patch Numeric. Whenever you call a method that doesn't exist, it calls the method "method_missing". Hence you can monkey patch Numeric#method_missing to call the equivalent Math function.