Constrained genericity: Difference between revisions
Content added Content deleted
m (J: remove stale intro quip, simplify, and add some narrative description) |
(→{{header|Ruby}}: Added Ruby) |
||
Line 1,103: | Line 1,103: | ||
All the tests pass. Look at the tests to see what generates an exception (i.e. |
All the tests pass. Look at the tests to see what generates an exception (i.e. |
||
not allowed at runtime) and what does not. |
not allowed at runtime) and what does not. |
||
=={{header|Ruby}}== |
|||
<lang ruby>class Foodbox |
|||
def initialize (*food) |
|||
raise ArgumentError, "food must be eadible" unless food.all?{|f| f.respond_to?(:eat)} |
|||
@box = food |
|||
end |
|||
end |
|||
class Fruit |
|||
def eat; end |
|||
end |
|||
class Apple < Fruit; end |
|||
p Foodbox.new(Fruit.new, Apple.new) |
|||
# => #<Foodbox:0x00000001420c88 @box=[#<Fruit:0x00000001420cd8>, #<Apple:0x00000001420cb0>]> |
|||
p Foodbox.new(Apple.new, "string can't eat") |
|||
# => test1.rb:3:in `initialize': food must be eadible (ArgumentError) |
|||
</lang> |
|||
=={{header|Sather}}== |
=={{header|Sather}}== |