Delegate - KhanhLD

234 views
190 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
234
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Delegate - KhanhLD

  1. 1. Delegate in Ruby on Rails
  2. 2. ● Delegate ● Options ● Demo
  3. 3. Delegate ● Easily expose contained objects’ public methods as your own. ● Useful with ActiveRecord associations ● delegate :method_name, to: :target
  4. 4. Delegate ● Multiple delegates to the same target are allowed ● delegate :method1, :method2, to: :target ● Methods can be delegated to instance variables, class variables, or constants
  5. 5. Delegate ● Delegate a method to the class by using :class ● delegate :hello, to: :class
  6. 6. Options ● :allow_nil ● If the target is nil and does not respond to the delegated method a NoMethodError is raised ● delegate :name, to: :profile, allow_nil: true ● Returns nil
  7. 7. Options ● delegate :name, to: :profile, prefix: true ● Delegate methods are prefixed with the name of the object being delegated to. ● delegate :name, to: :client, prefix: :customer ● Supply a custom prefix
  8. 8. Thank you

×