interface_exists

  • There are no interfaces in Ruby the way there is in PHP. Ruby uses class inheritance or module mixins to specify an interface for an object. Both class and module names are constants, so we can check if either of these exist by verifying that the constant exists for the class or module name using Module.const_defined?.

    PHP

    interface Carnivore {}
     
    $exists = interface_exists('Carnivore');
    var_export($exists);
    // => true

    Ruby

    class Dinosaur; end
    module Carnivore; end
     
    p Module.const_defined?("Dinosaur")  # => true
    p Module.const_defined?("Carnivore") # => true

    see also

in_array is_a

Looking for Rails or PHP web application development, integration, and training?

Rails for PHP is brought to you by Maintainable Software. Get custom web applications and personalized training from the authors of the book and website.