热门标签 | 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


推荐阅读
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 本文详细介绍了Java编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • ImmutableX Poised to Pioneer Web3 Gaming Revolution
    ImmutableX is set to spearhead the evolution of Web3 gaming, with its innovative technologies and strategic partnerships driving significant advancements in the industry. ... [详细]
  • 本文介绍如何使用阿里云的fastjson库解析包含时间戳、IP地址和参数等信息的JSON格式文本,并进行数据处理和保存。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 深入解析Spring Cloud Ribbon负载均衡机制
    本文详细介绍了Spring Cloud中的Ribbon组件如何实现服务调用的负载均衡。通过分析其工作原理、源码结构及配置方式,帮助读者理解Ribbon在分布式系统中的重要作用。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文探讨了MariaDB在当前数据库市场中的地位和挑战,分析其可能面临的困境,并提出了对未来发展的几点看法。 ... [详细]
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社区 版权所有