是什么决定,如果对一个可变类一声巨响方法返回`nil`? [英] What determines if a bang method on a mutable class returns `nil`?

查看:108
本文介绍了是什么决定,如果对一个可变类一声巨响方法返回`nil`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一般情况下,在可变类爆炸的方法,如字符串阵列哈希收益时不能修改的。但有些阵列爆炸的方法,即收集!地图!逆转!旋转!洗牌!排序! sort_by!哈希爆炸方法,即合并!,有去无回。这背后的原因是什么?是什么让与别人不同的这些方法?我不明白为什么知道一个数组是否由排序排序!是没有用的,而知道一个数组是否由制作独特的uniq!是很有用的。


解决方案

TL; DR


  

[b]上可变类陈子昂方法[西]回零时,没有修改,就...... [B] UT斯达康一些阵列爆炸方法...永远不会返回零。


如果有一个官方理由(沿一个官方规范的线),我目前不知道它。我个人怀疑它只是因为某些对象使用零作为返回值来指示错误(如索引超出范围),而不是抛出一个异常,而另一些人总是返回一个有效的对象。我很怀疑有一个压倒一切的理念在那里,虽然似乎有,当你挖得足够深时使用爆炸方法的普遍共识。

浜方法不是天生关于突变

作为一个例子

问题#5009 ,其中要求:


  

[P]租赁使用爆炸的方法(那些结尾!)一贯的API。


有用的回复说:


  

[那爆炸是破坏性的]是对使用Ruby的爆炸方法一种常见的误解。爆炸并不表明方法变异的接收器,只不过是表示应谨慎使用。


部分社区共识爆炸方法

肯定是有关于何时使用爆炸方法Ruby开发者之间达成一些共识。目前,红宝石风格指南提供了以下指导原则:


  

      
  • 有潜在危险的方法的名称......应以感叹号结尾,如果存在这种危险方法的安全版本。

  •   
  • 定义非爆炸(安全)方法在爆炸的条款(危险的)一个,如果可能的。

  •   

这些准则似乎与一般的想法是一致的爆炸方法对来电者是在选择方法或处理的返回值,而不是作为什么将返回一个指标爆炸小心。

Usually, bang methods on mutable a class such as String, Array, or Hash return nil when no modification is made. But some Array bang methods, i.e., collect!, map!, reverse!, rotate!, shuffle!, sort!, sort_by! and a Hash bang method, i.e., merge!, never return nil. What is the rationale behind this? What makes these methods different from others? I don't see why knowing whether an array was sorted by sort! is not useful while knowing whether an array was made unique by uniq! is useful.

解决方案

TL;DR

[B]ang methods on mutable class[es] return nil when no modification is made...[b]ut some Array bang methods...never return nil.

If there is an "official" rationale (along the lines of an official specification), I'm currently unaware of it. I personally suspect it's simply because some objects use nil as a return value to indicate errors (e.g. index out of range) rather than raising an exception, while others always return a valid object. I very much doubt there's an overriding philosophy there, although there appears to be a general consensus about when to use bang methods when you dig deep enough.

Bang Methods Aren't Inherently About Mutation

As one example, consider issue #5009, which requests:

[P]lease use bang methods (those that end with !) consistently in the API.

One useful response says:

[That bang is destructive] is a common misconception about the use of bang methods in Ruby. Bang does not indicate that a method mutates its receiver, merely that it should be used with caution.

Some Community Consensus on Bang Methods

There is definitely some consensus among Rubyists about when to use bang methods. The Ruby Style Guide currently offers the following guidelines:

  • The names of potentially dangerous methods...should end with an exclamation mark if there exists a safe version of that dangerous method.
  • Define the non-bang (safe) method in terms of the bang (dangerous) one if possible.

These guidelines seem consistent with the general idea that bang methods are about the caller being careful in choosing the method or handling the return value, rather than the bang acting as an indicator of what will be returned.

这篇关于是什么决定,如果对一个可变类一声巨响方法返回`nil`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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