Created
November 26, 2014 11:10
-
-
Save senny/2f6b21c1a33fe3b0b813 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Developer | |
def salery | |
5000 | |
end | |
end | |
class Manager | |
def salery | |
8000 | |
end | |
end | |
class Firm | |
def initialize | |
@employees = [] of Object | |
end | |
def add_employee(emp : Object) | |
if emp.responds_to?(:salery) | |
@employees << emp | |
end | |
end | |
def total_salery | |
@employees.sum(&.salery) | |
end | |
end | |
firm = Firm.new | |
firm.add_employee Developer.new | |
firm.add_employee Manager.new | |
puts firm.total_salery |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Error in /Users/senny/Projects/playground/crystal/first_steps/objects.cr:36: instantiating 'Firm#total_salery()' | |
puts firm.total_salery | |
^~~~~~~~~~~~ | |
in /Users/senny/Projects/playground/crystal/first_steps/objects.cr:29: instantiating 'Array(Object+)#sum()' | |
@employees.sum(&.salery) | |
^~~ | |
in /usr/local/Cellar/crystal/0.5.3/src/enumerable.cr:291: instantiating 'inject(Int32)' | |
inject(initial) { |memo, e| memo + (yield e) } | |
^~~~~~ | |
in /usr/local/Cellar/crystal/0.5.3/src/enumerable.cr:53: instantiating 'each()' | |
each do |elem| | |
^~~~ | |
in /usr/local/Cellar/crystal/0.5.3/src/enumerable.cr:53: instantiating 'each()' | |
each do |elem| | |
^~~~ | |
in /usr/local/Cellar/crystal/0.5.3/src/enumerable.cr:291: instantiating 'inject(Int32)' | |
inject(initial) { |memo, e| memo + (yield e) } | |
^~~~~~ | |
in /Users/senny/Projects/playground/crystal/first_steps/objects.cr:29: instantiating 'Array(Object+)#sum()' | |
@employees.sum(&.salery) | |
^~~ | |
in /Users/senny/Projects/playground/crystal/first_steps/objects.cr:29: undefined method 'salery' for Reference | |
@employees.sum(&.salery) | |
^~~~~~ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can define an abstract super class.