4 ms·
I've digged a bit on the subject (better late than never), it seems that duck typing is seen in ruby as a guideline, rather than a language feature. Basically,
by oelmekki 8y ago
I've digged a bit on the subject (better late than never), it seems that duck typing is seen in ruby as a guideline, rather than a language feature. Basically, it's about recommending to use `#respond_to?` rather than `#is_a?`, `#kind_of?` or `Class#===` in end user code.
- vidarh 8y agoIt's a language feature in as much as the absence of static typing and the ability to check for methods is what makes it possible. But yes, it it a guideline, and Ruby code that violates it will generally be seen as not being idiomatic Ruby.
- oelmekki 8y agoOh, I see, given the interpreter is written in strongly typed language, allowing to go without static typing is a feature by itself, and allowing to test for methods presence allows to do duck typing, thus it allows to say that ruby implements duck typing. It makes more sense than talking of duck typing explicit usage by end users, given it's quite rare we assert proper received parameters extensively. EDIT : which makes me realize we have a machine language without typing (binary), on top of which we use a language implementing strong typing (C), used to build a language without strong typing (ruby), on top of which Stripe is building strong typing. Making a definitive decision is hard :)
- mdpye 8y agoNitpick: C is weakly typed, Ruby is strong. C is statically typed, Ruby is dynamic. Strong typing is about objects having a definite type. In Ruby, you cannot cast a String to a Fixnum, an exception will be raised. But in C you may cast anything to anything really, and your program will believe you, whether it could possibly make sense or not...
- oelmekki 8y agoI thought "strong typing" and "static typing" were interchangeable. I stand corrected, thanks.