热门标签 | 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编程语言中的核心概念和常见面试问题,包括集合类、数据结构、线程处理、Java虚拟机(JVM)、HTTP协议以及Git操作等方面的内容。通过深入分析每个主题,帮助读者更好地理解Java的关键特性和最佳实践。 ... [详细]
  • PyCharm下载与安装指南
    本文详细介绍如何从官方渠道下载并安装PyCharm集成开发环境(IDE),涵盖Windows、macOS和Linux系统,同时提供详细的安装步骤及配置建议。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 尽管某些细分市场如WAN优化表现不佳,但全球运营商路由器和交换机市场持续增长。根据最新研究,该市场预计在2023年达到202亿美元的规模。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 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. ... [详细]
  • 2023年京东Android面试真题解析与经验分享
    本文由一位拥有6年Android开发经验的工程师撰写,详细解析了京东面试中常见的技术问题。涵盖引用传递、Handler机制、ListView优化、多线程控制及ANR处理等核心知识点。 ... [详细]
  • libsodium 1.0.15 发布:引入重大不兼容更新
    最新发布的 libsodium 1.0.15 版本带来了若干不兼容的变更,其中包括默认密码散列算法的更改和其他重要调整。 ... [详细]
  • 深入解析 Apache Shiro 安全框架架构
    本文详细介绍了 Apache Shiro,一个强大且灵活的开源安全框架。Shiro 专注于简化身份验证、授权、会话管理和加密等复杂的安全操作,使开发者能够更轻松地保护应用程序。其核心目标是提供易于使用和理解的API,同时确保高度的安全性和灵活性。 ... [详细]
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社区 版权所有