替换“&"到“"在 Ruby 中似乎不可能? [英] Replace "&" to "&" in Ruby seems impossible?

查看:15
本文介绍了替换“&"到“"在 Ruby 中似乎不可能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 String.gsub(或其他方法)将所有 & 字符替换为 &.我尝试了几种组合并阅读了这里的另一个问题,但没有任何效果.

I want to replace all & characters into & with String.gsub (or a other method). I've tried several combinations and read another question here, but nothing is gonna work.

  "asdf & asdf".gsub("&", "\&") => "asdf & asdf"

推荐答案

您链接的问题提供了解决方案 - 使用 gsub 的块形式:

Your linked question provides a solution - use the block form of gsub:

irb(main):009:0> puts "asdf & asdf".gsub("&"){'&'}
asdf & asdf

这篇关于替换“&"到“"在 Ruby 中似乎不可能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆