Skip to content

Instantly share code, notes, and snippets.

@KamilLelonek
Created March 27, 2015 12:12
Show Gist options
  • Save KamilLelonek/392eef218c124a7a1f7d to your computer and use it in GitHub Desktop.
Save KamilLelonek/392eef218c124a7a1f7d to your computer and use it in GitHub Desktop.
How to delegate methods in Ruby
require 'active_support/core_ext/module/delegation'
class LazyEmployee
def initialize(sandwich_maker)
@sandwich_maker = sandwich_maker
end
delegate :make_me_a_sandwich, to: :sandwich_maker
private
attr_reader :sandwich_maker
end
[1] (pry) main: 0> sandwich_maker = SandwichMaker.new
=> #<SandwichMaker:0x007fd2da810d30>
[2] (pry) main: 0> lazy_employee = LazyEmployee.new(sandwich_maker)
=> #<LazyEmployee:0x007fd2da9a5b78 @sandwich_maker=#<SandwichMaker:0x007fd2da810d30>>
[3] (pry) main: 0> lazy_employee.make_me_a_sandwich
OKAY
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment