0

I was reading "Back to Basics: Polymorphism and Ruby" and it seems like they define polymorphism as:

Polymorphism - the provision of a single interface to entities of different types

which sounds a lot like

duck-typing: In computer programming with object-oriented programming languages, duck typing is a layer of programming language and design rules on top of typing. Typing is concerned with assigning a type to any object. Duck typing is concerned with establishing the suitability of an object for some purpose. With normal typing, suitability is assumed to be determined by an object's type only. In duck typing, an object's suitability is determined by the presence of certain methods and properties (with appropriate meaning), rather than the actual type of the object.

According to the link, duck-typing seems to be when a method behaves differently depending on the object that receives its message. But that sounds a lot like polymorphism, no?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Jwan622
  • 11,015
  • 21
  • 88
  • 181
  • 1
    http://stackoverflow.com/questions/11502433/what-is-the-difference-between-polymorphism-and-duck-typing?rq=1 , http://stackoverflow.com/questions/19165430/how-to-work-with-ruby-duck-typing?rq=1 and http://stackoverflow.com/questions/137661/how-do-you-do-polymorphism-in-ruby?rq=1 are pertinent. – the Tin Man Dec 16 '15 at 19:49

1 Answers1

1

Ruby being loosely dynamically-typed and interpreted language, there is no strict type checking type checking is done at runtime. When you invoke a method on an object, as long as it has that method defined - you will be able to invoke it and Ruby will not complain. This is duck-typing - "It's a duck if it quacks like a duck" - in other words, "If an object responds to all the methods of a class, then, may be its type is that class". Ruby doesn't really care much about classes - it's all about calling methods on objects.

You can't really demonstrate polymorphism in stricter sense as can be done in strongly statically-typed compiled languages like Java where an object's class must be either of the type of the variable it is being assigned to or must be a derived class of variable's type or implement the interface if the variable's type is interface.

One can verify whether an object's type is a specific class if need be as explained in "How do I check if a variable in an instance of a class?".

You may also want to read discussion to understand the terminologies used to characterize languages based on the type system.

Community
  • 1
  • 1
Wand Maker
  • 18,476
  • 8
  • 53
  • 87
  • Matz : "Ruby is dynamic (interpreted), strongly typed (mostly), dynamically typed (known as duck typing)"(https://searchcode.com/codesearch/view/15583534/) – steenslag Dec 16 '15 at 19:40
  • 1
    Please, don't spread misinformation. Even if that misinformation comes from the ruby-lang website. There is no such thing as an interpreted language. A language is an abstract mathematical object, a set of logical rules and restrictions. A language isn't compiled or interpreted. A language just *is*. Compilation or interpretation are traits of the compiler or interpreter (duh!), not the language. Case in point: all currently existing implementations of Ruby are compiled: YARV compiles Ruby to YARV bytecode, Rubinius compiles Ruby to Rubinius bytecode which it then translates to Rubinius IR … – Jörg W Mittag Dec 16 '15 at 21:11
  • 1
    … which it then translates to LLVM IR which it then compiles to native code, so Rubinius actually contains *at least* two compilers and up to 4 or even more depending on how you count. JRuby compiles to JRuby IR which it then further compiles to JVM bytecode, even ahead-of-time if you want to. JRuby+Truffle+Graal compiles to native code. Topaz compiles to Topaz bytecode which it then further compiles to native code. Opal compiles to ECMAScript. MagLev compiles to Gemstone/S bytecode which it then further compiles to native code. MRuby compiles to MRuby bytecode. RubyMotion compiles to native … – Jörg W Mittag Dec 16 '15 at 21:15
  • 1
    … code. IronRuby compiles to DLR trees which the DLR then compiles to CIL intcode. The only production-quality Ruby implementation in the history of Ruby that had no compiler was MRI, which is now obsolete and unmaintained. – Jörg W Mittag Dec 16 '15 at 21:16
  • Also, please don't use badly-defined terms like "loosely-typed" and "strict type checking" without defining them, so we know what you are talking about. Unlike the terms "static", "dynamic", "explicit" and "implicit", those terms ("strict", "loose", "weak", "strong") do not have a widely agreed-upon definition. Everybody has their own definition, and if you don't provide *yours*, then the term is meaningless. – Jörg W Mittag Dec 16 '15 at 21:20
  • @JörgWMittag: I've been using Ruby for about 6 months. I like to think I'm at least decent at it, but now you've made me confused. When I type `ruby` into my terminal and pass it a `*.rb` as an argument. What am I using? – Charles Dec 17 '15 at 00:29
  • @c650: Current versions of OSX come with a version of YARV pre-installed. Older versions of OSX come with a version of MRI pre-installed. Sometime in between, they also had an additional private version of MacRuby pre-installed. So, if you are using OSX, the answer is probably YARV if you didn't install anything yourself, otherwise it's whatever you installed. If you are using another operating system, it's whatever you installed, since to my knowledge no current mainstream OS comes with a Ruby implementation pre-installed. – Jörg W Mittag Dec 17 '15 at 09:25
  • @JörgWMittag I have RVM. So... is my Ruby code actually being compiled these days? – Charles Dec 17 '15 at 17:04
  • @c650: RVM is just a tool for installing and managing Ruby implementations, it is not a Ruby implementation. I cannot tell which Ruby implementations you installed using RVM, so I cannot answer your question. However, like I said, there has only been a single Ruby implementation without a compiler in the entire history of Ruby, namely MRI, and that implementation is no longer being developed, maintained, or supported for almost two and a half years now. So, unless you installed this very old and unmaintained version, yes, you are using a Ruby implementation with a compiler. The most likely … – Jörg W Mittag Dec 18 '15 at 13:04
  • @JörgWMittag Please feel free to add to the answer, I have made it community wiki – Wand Maker Dec 18 '15 at 13:27
  • … guess is that you are using YARV, which compiles Ruby sourcecode to YARV bytecode which it then interprets. You can check which Ruby implementation you are using, by checking the value of the global constant `RUBY_ENGINE`. It will be something like `'jruby'` for JRuby, `'ironruby'` for IronRuby, `'mruby'` for MRuby, `'topaz'` for Topaz, and so on. Confusingly, YARV will just say `'ruby'`, even more confusingly, MRI will *also* just say `'ruby'`. – Jörg W Mittag Dec 18 '15 at 13:32
  • @JörgWMittag: I am very aware that RVM is just a version manager. I just figured it had a default Ruby implementation. – Charles Dec 18 '15 at 18:01
  • @JörgWMittag also, would that mean that "interpreted" languages aren't really interpreted, or is this limited to Ruby? – Charles Dec 19 '15 at 19:07
  • @c650: This question is non-sensical, it cannot even be answered with "No", because that would imply that the question makes sense to be asked. There is no such thing as an interpreted language. Period. A language is an abstract mathematical object, a set of logical rules and restrictions. A language isn't compiled or interpreted. A language just is. Compilation or interpretation are traits of the compiler or interpreter (duh!), not the language. If English were a typed language, the term "interpreted language" would be a type error. Every language can be implemented with a compiler and … – Jörg W Mittag Dec 20 '15 at 04:21
  • … every language can be implemented with an interpreter. Most languages have both interpreted and compiled implementations. Most modern high-performance language implementations combine compilers and interpreters in a single implementation. There are compilers for Ruby (e.g. Opal, JRuby), ECMAScript (e.g. V8), and PHP (Quercus, P8). There are interpreters for C and C++ (Cint, Cling). JRuby started out as a pure interpreter, and later they added a JIT and an AOT compiler for performance reasons. IronRuby started out as a pure compiler, and later they added an interpreter for performance reasons – Jörg W Mittag Dec 20 '15 at 04:25