How do write two methods with different number of arguments in Ruby -
i trying write inside class:
class << self def steps @steps.call end def transitions @transitions.call end def steps(&steps) @steps = steps end def transitions(&transitions) @transitions = transitions end end
that won't work since in ruby, can't kind of method overloading. there way around this?
you can kind of method aliasing , mixins, way handle methods different signatures in ruby optional arguments:
def steps(&block) block.present? ? @steps = block : @steps.call end
this sort of delegation code smell, though. means there's awkward interface you've designed. in case, better:
def steps @steps.call end def steps=(&block) @steps = block end
this makes clear other objects in system how use interface since follows convention. allows other cases, passing block steps
method other use:
def steps(&block) @steps.call(&block) end
Comments
Post a Comment