Created
January 26, 2017 04:35
-
-
Save decors/a1ba2b5e4a700235b0d993378836d7b4 to your computer and use it in GitHub Desktop.
Crystal-lang simple delegator
This file contains 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 Delegator(T) | |
def initialize(@object : T) | |
end | |
def self.delegate(object) | |
new(object) | |
end | |
forward_missing_to @object | |
end | |
class Person | |
def greet | |
puts "Hi" | |
end | |
end | |
class PersonDelegator < Delegator(Person) | |
def greet_twice | |
2.times do greet end # call Person class method | |
end | |
end | |
person = Person.new() | |
person = PersonDelegator.delegate(person) | |
person.greet_twice # => Print "Hi" two times | |
person = Person.new() | |
person = Delegator(Person).new(person) | |
person.greet # "Hi" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment