Ruby .reject! vs .delete_if [英] Ruby .reject! vs .delete_if

查看:134
本文介绍了Ruby .reject! vs .delete_if的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Ruby的新手,我有一个关于.reject之间的区别的问题!和.delete_if方法处理哈希和数组时。如果只是想摆脱某些对象,那么这些方法在功能上是否有区别?和理由使用其中一个?



谢谢!



编辑
我已阅读文档......我想我应该在我原来的问题中更加清楚。我想知道更多关于效率的差异。他们在如何删除商品方面的操作方式不同? (同样,忽略返回值,我明白这是一个区别,谢谢!) 解决方案

文档对此很清楚。



区别在于如果 reject!不改变数组,它会返回 nil delete_if 将返回未更改的数组。


Being new to Ruby, I have a question about the difference between the .reject! and .delete_if methods when dealing with hashes and arrays. If just wanting to get rid of certain objects, is there functionally any difference between the methods? And reason to use one over the other?

Thanks!

EDIT I have read the documentation...I guess I should have been more clear in my original question. I was wondering more about differences in efficiency. Do they operate differently in how they delete items? (Again, ignoring return value. I understand that is a difference. Thanks!)

解决方案

The documentation is pretty clear about this.

The difference is that if reject! does not change the array, it returns nil. delete_if will return the unchanged array.

这篇关于Ruby .reject! vs .delete_if的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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