picardo
picardo

Reputation: 24886

How can I delegate calls to a method to another method in Ruby?

I want to create another name for a method that already exists, e.g. I want to call slice with only. I don't want to change anything about the behavior of that original method, so it's not a redefinition, but delegation. How can I do that?

Upvotes: 0

Views: 66

Answers (2)

HungryCoder
HungryCoder

Reputation: 7616

There are a number of ways to do that in Ruby. Please check this post which may help you find your most suitable way of doing it.

http://gdakram.com/past/2010/12/2/multiple_ways_in_implementing_delegation_pattern_in_ruby/

Upvotes: 1

Peter Lundgren
Peter Lundgren

Reputation: 9197

class Whatever
  def slice
    # do something
  end
  alias :only :slice
end

Upvotes: 1

Related Questions