
Originally Posted by
matsko
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"

Originally Posted by
matsko
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
Bookmarks