fang358229166 2015-10-18 02:18 采纳率: 0%
浏览 755

ruby 模块中函数参数错误

![![![图片说明](https://img-ask.csdn.net/upload/201510/18/1445134618_560989.png)图片说明](https://img-ask.csdn.net/upload/201510/18/1445134669_926112.png)图片说明](https://img-ask.csdn.net/upload/201510/18/1445134588_663292.png)图片说明
问题如题,谢谢

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-08-04 12:36
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    在Ruby中,expect方法用于验证一个字符串是否为回文。然而,在你的测试脚本中,出现了几个问题。这些问题导致了错误的预期结果。以下是修复这些错误的代码:

    def is_palindrome(str)
        str.downcase.gsub(/[^a-z0-9]/i, '').strip == str.downcase.strip.reverse
    end
    
    require_relative "fun_with_strings"
    
    describe "Palindrome Detection" do
      it "should work for simple strings" do
        expect("redivider").to be_palindrome
      end
    
      it "should be case-insensitive" do
        expect("ReDivider").to be_palindrome
      end
    
      it "should ignore non-word characters" do
        expect("A man, a plan, a canal -- Panama").to be_palindrome
      end
    end
    

    在这个解决方案中,我们定义了一个名为 is_palindrome 的方法来检查给定的字符串是否是回文。然后,我们在 describe 方法中编写了三个不同的测试用例来验证我们的函数。

    注意:这个解决方案假设你已经安装并使用了 fun_with_strings 文件夹中的所有模块,并且它们已经被正确导入到当前的工作空间中。如果你还没有这样做,请确保按照说明进行操作。

    评论

报告相同问题?