热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

to_a和to_ary有什么区别?-What'sthedifferencebetweento_aandto_ary?

Whatsthedifferencebetweento_aandto_ary?to_a和to_ary有什么区别?

What's the difference between to_a and to_ary?

to_a和to_ary有什么区别?

4 个解决方案

#1


40  

to_ary is used for implicit conversions, while to_a is used for explict conversions.

to_ary用于隐式转换,而to_a用于explict转换。

For example:

例如:

class Coordinates
  attr_accessor :x, :y

  def initialize(x, y); @x, @y = x, y end

  def to_a; puts 'to_a called'; [x, y] end

  def to_ary; puts 'to_ary called'; [x, y] end

  def to_s; "(#{x}, #{y})" end

  def inspect; "#<#{self.class.name} #{to_s}>" end
end

c = Coordinates.new 10, 20
# => #

The splat operator (*) is a form of explicit conversion to array:

splat运算符(*)是一种显式转换为数组的形式:

c2 = Coordinates.new *c
# to_a called
# => #

On the other hand, parallel assignment is a form of implicit conversion to array:

另一方面,并​​行赋值是一种隐式转换为数组的形式:

x, y = c
# to_ary called
puts x
# 10
puts y
# 20

And so is capturing collection members in block arguments:

在块参数中捕获集合成员也是如此:

[c, c2].each { |(x, y)| puts "Coordinates: #{x}, #{y}" }
# to_ary called
# Coordinates: 10, 20
# to_ary called
# Coordinates: 10, 20

Examples tested on ruby-1.9.3-p0.

在ruby-1.9.3-p0上测试的实施例。

This pattern seems to be used all over the Ruby language, as evidenced by method pairs like to_s and to_str, to_i and to_int and possibly more.

这种模式似乎在整个Ruby语言中使用,如to_s和to_str,to_i和to_int等可能更多的方法对所证明。

References:

参考文献:

  • Ruby Issue 3680
  • Ruby Issue 3680
  • Variables
  • 变量

#2


19  

to_ary allows an object to be treated as an array, whereas to_a actually tries to convert the parameter into an array.

to_ary允许将对象视为数组,而to_a实际上尝试将参数转换为数组。

to_ary can be useful for parallel assignment, whereas to_a is more suited for an actual conversion.

to_ary对并行赋值很有用,而to_a更适合实际转换。

#3


12  

Quoted from gabew's web space:

引自gabew的网络空间:

Calling #to_a will convert the receiver to an Array, while #to_ary will not.

调用#to_a会将接收器转换为数组,而#to_ary则不会。

ruby-1.9.2-p290 :001 > class A  A[].to_a.class
 => Array

ruby-1.9.2-p290 :005 > A[].to_ary.class
 => A 

#4


0  

to_a, when called on an object returns an array representation of obj

to_a,当在一个对象上调用时,返回一个obj的数组表示

Examples

例子

class Example
  def initialize
      @str = 'example'
  end
end

a = Example.new #=> # [#]

Hash Example

哈希示例

h = { "c" => 300, "a" => 100, "d" => 400, "c" => 300  } 
h.to_a   #=> [["c", 300], ["a", 100], ["d", 400]]

An array can also be created by using the Array() method, provided by Kernel, which tries to call to_ary, then to_a on its argument. http://ruby-doc.org/core-2.0/Array.html#method-i-to_ary

也可以使用由Kernel提供的Array()方法创建一个数组,该方法尝试调用to_ary,然后调用to_a的参数。 http://ruby-doc.org/core-2.0/Array.html#method-i-to_ary

so as far as I can see, the Array#to_ary src just returns the array that is passed in, as in

所以我可以看到,Array#to_ary src只返回传入的数组,如

def to_ary
  return self
end

if I understand correctly, to_a is used for array conversion and makes its final return using to_ary. But this may not be true in future versions according to apidock

如果我理解正确,to_a用于数组转换并使用to_ary进行最终返回。但根据apidock的说法,未来的版本可能并非如此

to_a Returns an array representation of obj. For objects of class Object and others that don’t explicitly override the method, the return value is an array containing self. However, this latter behavior will soon be obsolete. http://apidock.com/ruby/Object/to_a

to_a返回obj的数组表示形式。对于Object类的对象和其他未显式覆盖该方法的对象,返回值是一个包含self的数组。但是,后一种行为很快就会过时。 http://apidock.com/ruby/Object/to_a


推荐阅读
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • Windows服务与数据库交互问题解析
    本文探讨了在Windows 10(64位)环境下开发的Windows服务,旨在定期向本地MS SQL Server (v.11)插入记录。尽管服务已成功安装并运行,但记录并未正确插入。我们将详细分析可能的原因及解决方案。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 深入理解 SQL 视图、存储过程与事务
    本文详细介绍了SQL中的视图、存储过程和事务的概念及应用。视图为用户提供了一种灵活的数据查询方式,存储过程则封装了复杂的SQL逻辑,而事务确保了数据库操作的完整性和一致性。 ... [详细]
  • c# – UWP:BrightnessOverride StartOverride逻辑 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
author-avatar
干杯彩妮子
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有