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
精彩评论