SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    $books++ == true matsko's Avatar
    Join Date
    Sep 2004
    Location
    Toronto
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Interfaces and Type Hinting?

    I'm just starting out with Ruby and I've got a question about the OO features of the language.

    Does Ruby support native interfaces, or do you need to code your own base or mixin setup?

    And if you do manage to create a interface then is it possible to setup a type hint within a the parameters of a function to only allow a specific type of Class or Interface to be accepted?

    sub someFunc(Class a)

    end

    Is this possible?
    I can't believe I ate the whole thing

  2. #2
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by matsko View Post
    Does Ruby support native interfaces, or do you need to code your own base or mixin setup?
    I think your question relates to inheritance. You can create a new class based on an existing class very easily in Ruby. You can also mixin other code using modules
    Code:
    class Animal
      def breath
        puts "Puff, puff"
      end
    end
    
    module FourLeggedAnimals
      def legs
        puts "4"
      end
    end
    
    class Dog < Animal
      include FourLeggedAnimals
      def bark
        puts "Woof, woof"
      end
    end
    
    dog = Dog.new
    dog.breath   --->  "Puff, puff"
    dog.bark     --->   "Woof, woof"
    dog.legs      --->   "4"
    Quote Originally Posted by matsko View Post
    And if you do manage to create a interface then is it possible to setup a type hint within a the parameters of a function to only allow a specific type of Class or Interface to be accepted?
    Ruby is not strongly typed so no you cannot specify the type of input into a method in the way you suggest. However, you can determine the type of object passed in within the code and alter the behaviour to suit:
    Code:
    def get_something(something)
      # version that will only find Dogs
      if something.class == Dog
        # do dog only things
      elsif something.kind_of? Animal
        # do animal things
      else
        raise "Item is not an animal"
      end
    end
    Note that kind_of? will return true for objects that inherit from the class. So:
    Code:
    dog = Dog.new
    dog.class == Animal    ---> false
    dog.kind_of? Animal    ---> true
    See the Object api for more details of class and kind_of? methods:

    http://www.ruby-doc.org/core/classes/Object.html

  3. #3
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    I believe the answer to your question is duck typing.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ReggieB View Post
    Ruby is not strongly typed so no you cannot specify the type of input into a method in the way you suggest.
    Actually, Ruby is strongly typed. It's not statically typed however. And indeed, there is no way to enforce the type of a parameter in Ruby. The only way to get explicit interfaces in Ruby, is by documenting them. You can't have the runtime enforce it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •