开发者

Call super for self

I have a 3rd party gem with extension for String class:

class String
  def to_url
    self.gsub /\s+/, '-'
  end
end

And I have my app trying to extend String class:

class String
  def to_url
    Russian.translit self
    super
  end
end

How do I call super (to replace spaces AND do transliteration) from my app? My code does super, but skips Russian.translit sel开发者_Go百科f.


There is no super to call. You should use alias_method

class String
  alias_method :old_to_url, :to_url
  def to_url
    Russian.translit(self).old_to_url
  end
end


I think that your Russian.translit self is working fine you are just not using the result. You should be using something like Russian.translit! self if Russian has such a method.


Yay! I found a way to call super for self (if there were a super). self cannot be assigned, but it's data can be replaced.

class String
  def to_url
    self.replace Russian.translit(self)
    super
  end
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜