基于一个属性的不区分大小写的 equals 方法

2024-02-10

原始问题

这是一个非常可怕的方法,它根据代码检查是否相等,但与大小写无关

def ==(another_country)
   (code.nil? ? nil : code.downcase) == (another_country.code.nil? ? nil : another_country.code.downcase) unless another_country.nil?
end

您能否为我指出正确的方向,如何编写这个更优雅的代码而不依赖于丑陋的 if else 结构?

这是我最终使用的解决方案(+RSpecs)

# Country model
class Country < ActiveRecord::Base
  attr_accessible :code

  def ==(another_country)
    code.to_s.downcase == another_country.code.to_s.downcase rescue false
  end
end

广泛的测试:

# RSpec
describe Country do
   describe 'equality based solely on Country.code' do
      before do
        @country_code_de = FactoryGirl.build(:country, :code => 'de')
      end

      it 'should be equal if Country.code is equal' do
        other_country_code_de = FactoryGirl.build(:country, :code => 'de')
        @country_code_de.should == other_country_code_de
      end

      it 'should be not equal if Country.code is not equal' do
        country_code_usa = FactoryGirl.build(:country, :code => 'usa')
        @country_code_de.should_not == country_code_usa
      end

      it 'should be case insensitive' do
        country_code_de_uppercase = FactoryGirl.build(:country, :code => 'DE')
        @country_code_de.should == country_code_de_uppercase
      end

      it 'should not rely on id for equality' do
        @country_code_de.id = 0
        country_code_usa = FactoryGirl.build(:country, :code => 'usa', :id => 0)
        @country_code_de.should_not == country_code_usa
      end

      it 'should be not equal if Country.code of one Country is nil' do
        country_code_nil = FactoryGirl.build(:country, :code => nil)
        @country_code_de.should_not == country_code_nil
      end

      it 'should be equal if Country.code for both countries is nil' do
        country_code_nil = FactoryGirl.build(:country, :code => nil)
        other_country_code_nil = FactoryGirl.build(:country, :code => nil)
        country_code_nil.should == other_country_code_nil
      end

      it 'should be not equal if other Country is nil' do
        @country_code_de.should_not == nil
      end

      it 'should be not equal if other object is not a Country' do
        @country_code_de.should_not == 'test'
      end

      it 'should be equal for descendants of Country with same Country.code' do
        class CountryChild < Country
        end
        country_child = CountryChild.new(:code => 'de')
        @country_code_de.should == country_child
      end
    end
end

这个怎么样,

def ==(another_country)
   return false if code.blank? # Remove this line if you want to return true if code and antoher_country.code are nil
   code.to_s.downcase == another_country.to_s.code.downcase rescue false
end

这里如果有的话code, another_country or another_country.code为零,它将通过一个异常并且rescue false语句将返回false value.

如果一切顺利,就会进行比较true or false将根据输入返回。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

基于一个属性的不区分大小写的 equals 方法 的相关文章

  • 验证 ActionCable 连接

    我发现了一个很棒的 ActionCable gem 它是 SPA 的一个很好的解决方案 我只想发送html css and js资产 所有其他连接将通过ActionCable 交换字符串或者整数并不难 但是如何通过ActionCable登录
  • GitHub API:标记提交所属(与 git describe --tag 并行)

    我正在使用 GitHub API 进行实验octokit https github com octokit octokit rb红宝石 我的目标是能够提取提交 SHA 所属的 标签 现在我可以使用命令行轻松地执行此操作 gt git des
  • 类、模块、它们的特征类和方法查找

    我们来开公开课吧Module并向其中添加一个方法 class Module def foo puts phew end end 我可以通过这样做来调用这个方法 Class foo 这是可以理解的 因为类Class is Class 其超类是
  • 在 Ruby 中跨多个类实例记忆数据的好方法是什么?

    考虑 生成数据的对象的许多实例 如果每次运行只生成一次该数据 那就太好了 class HighOfNPeriods lt Indicator def generate data indicator data DataStream new 0
  • 如何将两个不同的哈希数组中的值添加在一起?

    我有两个哈希数组 哈希值的键不同 player scores1 first name gt Bruce score gt 43 time gt 50 first name gt Clark score gt 45 minutes gt 20
  • rvm编译安装ruby 2.5.0出错

    我正在尝试使用 rvm 安装 ruby 2 5 0 但出现错误 我在 Ubuntu 18 16 和现在的 Linux Mint Cinnamon 上尝试过 基本上我在运行安装 ruby 的代码之前所做的是 打开 GPG 密钥https rv
  • 如何阻止与 RSpec 和 Capybara 的外部连接?

    在我的 Rails 项目中 我想编写非理想条件的测试 例如缺乏互联网连接或超时 例如 我正在使用 gem 来联系 API 并且希望确保在我的应用程序和外部 API 之间存在连接问题时能够正确处理错误 我已经可以通过用录像机制作固定装置并从
  • SSL_connect 返回=1 errno=0 状态=SSLv3 读取服务器证书 B:证书验证仅在代理时失败

    这篇文章几乎重复了许多其他帖子 包括Rails 4 和 Ruby 2 Net HTTP SSL 请求 OpenSSL SSL SSLError SSL connect returned 1 errno 0 state SSLv2 v3 re
  • “没有可用的二元红宝石”是什么意思?

    每当我使用rvm install x x x 即使安装成功 我也会收到此警告 No binary rubies available for osx 10 12 x86 64 ruby 2 4 0 Continuing with compil
  • 捆绑安装无法从 https://rubygems.org/ 获取规格

    我正在尝试遵循 Hartl Rails 教程 但在使用捆绑器 gem 时遇到了问题 使用命令 bundle install 或 bundle update 时 我得到以下输出 从中获取源索引https rubygems org https
  • 有没有人有 Ruby 和 Rake 的 Notepad++ 函数列表插件的解析规则

    我使用 Notepad 编辑 rake 文件 并且希望能够使用函数列表插件 我无法在线找到任何解析规则 并且 语言解析规则 对话框没有非常清晰的记录 我正在将方法解析到以下列表中 但还想显示任务 Function Begin t def t
  • 通过推送通知唤醒

    Suppose 有一些对象 例如 一个数组a 和依赖于对象的条件 例如 a empty 当前线程以外的某些线程可以操作该对象 a 因此条件评估值的真实性会随着时间的推移而变化 如何让当前线程在代码中的某个时刻休眠 并在条件满足时通过推送通知
  • 如何计算带有偏移量的异或?

    我想用不同的偏移量进行异或计算以在计算中列出 例子 key 0 1 0 text 0 1 0 1 0 1 0 1 1 1 异或计算 key 0 text 0 key 1 text 1 key 2 text 2 key 0 text 3 ke
  • yard 0.7.3 无法在 Markdown 和 Textile 中构建我的自述文件

    我决定将我的项目中的 README 文件转换为 Markdown 并一直使用yard 验证文档是否正确呈现 所以我安装了 rdiscount 将 README 更改为 README md 并尝试 yard doc README md 这给了
  • REXML - 如何提取单个元素

    我正在用 ruby 编写一些验收测试 其中涉及断言响应 XML 中值的存在 我的 XML 是这样的
  • rvm gem 安装错误?

    我正在摆弄 ruby gems 和 rvm 它工作得很好 但现在当我尝试安装 gem 时出现错误 gem install Rails错误 同时 执行 gem Errno EACCES 权限被拒绝 Users da rvm gems ruby
  • 在 ruby​​ 中下载多个 FTP 文件,如 d*.txt

    我需要连接到 ftp 站点并下载一堆名为 D txt 的文件 最多 6 个 你能帮我用 Ruby 编写这个代码吗 下面的代码只是 ftp Net FTP new ftp server site ftp login user pwd ftp
  • RoR - Rails 中的大文件上传

    我有一个 Rails Web 应用程序 允许用户上传视频 视频存储在 NFS 安装的目录中 当前的设置适用于较小的文件 但我也需要支持大文件上传 最多 4GB 当我尝试上传 4GB 文件时 它最终会发生 但从用户体验的角度来看很糟糕 上传开
  • 正则表达式的 o 修饰符是什么意思?

    Ruby 正则表达式有一些选项 例如i x m o i例如 意味着忽略大小写 什么是o选项是什么意思 在ri Regexp 它说o意味着执行 仅插值一次 但是当我这样做时 a one b a a two b不改变 它保持 one 我缺少什么
  • Watir Webdriver 加载 Chrome 扩展

    我正在尝试使用 Watir 加载 chrome 扩展 但遇到了问题 我发现这个相关问题 能够启动带有 watir webdriver 加载扩展的 chrome https stackoverflow com questions 125867

随机推荐