private method `gsub' called for 0:Fixnum

本文介绍了在Ruby中处理特殊字符实体转换时遇到的问题及两种解决方案:一是直接修改Ruby源码中的text.rb文件;二是将整数类型的属性值转换为字符串类型。这两种方法都能有效解决gsub不支持数字为属性值的标签转换问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gsub这个方法还不支持数字为属性值的标签转换……打开rexml文件夹下面的text.rb,找到代码,大概在289行

 

Ruby代码
  1. def Text::normalize( input, doctype=nil, entity_filter=nil )   
  2.       copy = input   
  3.       # Doing it like this rather than in a loop improves the speed   
  4.       #copy = copy.gsub( EREFERENCE, '&' )   
  5.       copy = copy.gsub( "&""&" )   
  6. ............省略后面的   
def Text::normalize( input, doctype=nil, entity_filter=nil )
      copy = input
      # Doing it like this rather than in a loop improves the speed
      #copy = copy.gsub( EREFERENCE, '&' )
      copy = copy.gsub( "&", "&" )
............

2种方法

 

1.修改Ruby,text.rb代码

        copy=input    =》         ruby = input.to_s  

2.直接将integer类型转换成string类型

解决!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值