我正在编写String类的扩展名:
#/lib/string.rb
Class String
def linkme
return self.gsub("[/link]", "</a>").gsub("[link=", "<a href='").gsub("]", "' target='_new'>")
end
end
然而,当我在控制台中调用它时,我得到了这个:
>>require 'lib/string'
SyntaxError: ./lib/string.rb:5: syntax error, unexpected kEND, expecting $end
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/local/lib/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from /home/path/to/vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in `require'
from /home/path/to/vendor/rails/activesupport/lib/active_support/dependencies.rb:521:in `new_constants_in'
from /home/path/to/vendor/rails/activesupport/lib/active_support/dependencies.rb:153:in `require'
from (irb):11
我认为这应该在理论上有效,因为我没有......
<geek_humor> loose "end"s </geek_humor>
这是我第一次尝试在Ruby中扩展这样的类,我觉得我做得很好,但也许S / O社区可以帮我把它包起来。
答案 0 :(得分:1)
这对你有用
class String
def linkme
self.gsub("[/link]", "</a>").gsub("[link=", "<a href='").gsub("]", "' target='_new'>")
end
end