哪里是Array()的文档? [英] where is the documentation for Array()?

查看:203
本文介绍了哪里是Array()的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浏览 ruleby 源$ C ​​$ C,我注意到,他们呼吁的集装箱(:和),这是我很少看到。事实上,唯一的其他地方我已经看到它在制造宝石

Browsing the ruleby source code, I noticed that they were calling Container(:and), which is something I rarely see. In fact, the only other place I've seen it is in the fabrication gem

有一个快速浏览一下发现,集装箱子类阵列,和快速跳到显示,阵列(:任何东西)#=> [:东西]

A quick look showed that Container subclasses Array, and a quick hop into Pry showed that Array(:anything) #=> [:anything].

另一个快速看一下Ruby文档的阵列没有按T阐明这个问题多少光。

Another quick look at the ruby documentation for Array doesn't shed much light on to this question.

是由叫什么法阵()时,它记录在哪里,怎么可以这样定义的方法,并且是它认为是坏表中的红宝石?

What method is being called by Array(), where is it documented, how can one define a method like this, and is it considered "bad form" in ruby?

推荐答案

有关你的第一个问题,答案是,它使得它的参数数组。

For your first question, the answer is that it makes an array of its parameters.

有关你的第二个,我不知道,但它足够简单。

For your second, I have no clue, but it's simple enough.

有关你的第三个,那就是:在Ruby中,定义具有相同名称的全局方法作为类被认为是用来构建或从类型更为基本类型的对象转换,只有当好形式。你不应该定义这个方法,除非你正在创建某种低的类型。你可以定义该像 BigInt128 一些类,但不是你应该为 ObscureOrSpecializedType678 。也有对这些方法的设计。

For your third, here it is: In Ruby, defining a global method with the same name as the class is considered good form only when used to construct or convert from object of that type of a more fundamental type. You should not define this method unless you are creating some sort of low type. You could define this for some class like BigInt128 but you shouldn't for ObscureOrSpecializedType678. There is also a design for these methods.

如果您要传递的数据是返回类型,返回它。如果数据是直接相关的类型,执行转换明显( Fixnum对象 BigInt128 )。如果传入的数据可以转换,是有点关系(字符串 Fixnum对象)将其转换(这种转换通常是只为字符串)。如果数据不能转换,抛出异常。你应该的从不返回一个魔法值。

If the data that you are passed is of the returned type, return it. If the data is of a directly related type, perform obvious conversions (Fixnum to BigInt128). If the data passed in can be converted and is somewhat related (String to Fixnum) convert it (this conversion is usually only for String). If the data can not be converted, throw an exception. You should NEVER return a "magic value".

另外使用这种方法是创建一个非字面类型半文字语法。这方面最好的例子是理性()复合()。这些功能,除了做转换,让您以更自然的方式创建口粮和复数(的Rational(1,2) Rational.new(1,2))。如果是简单的一类的文字重新presentation一定的参数列表,你定义一个类别名称()方法。

The other use of this method is to create a semi-literal syntax for non-literal types. The best examples of this are Rational() and Complex(). These functions, in addition to doing conversions, allow you to create rations and complex numbers in a more natural way (Rational(1, 2) vs. Rational.new(1, 2)). If there is a certain argument list that is simpler to the literal representation of a type, you would define a Classname() method.

在大多数情况下,这些方法仅仅是核心语言的一部分,除非你正在一类像 BigInt128 FancyString NaturalNumber ,你不应该定义这些方法。

For the most part, these methods are only part of the core language, and unless you are making a class like BigInt128 or FancyString or NaturalNumber, you should not define these methods.

据我所知,这些定义是:

From what I know the defined of these are:


  • 阵列(*参数) - 返回参数作为一个数组

  • 复合(实数,复数) - 建立一个具有给定的实和复杂零件的复数

  • 浮动(ARG) - 返回 ARG 转换为浮动(需要的东西像琴弦太)

  • 整数(ARG) - 同浮动(),而是转换为一个整数(花车被截断)

  • 理性(分子,分母= 1) - 使用给定的零件有理数

  • 字符串(ARG) - 通过调用参数转换为字符串 to_s

  • Array(*args) -- Returns arguments as an array
  • Complex(real, complex) -- Create a complex number with given real and complex parts
  • Float(arg) -- Returns arg converted to a float (takes things like strings too)
  • Integer(arg) -- Same as Float(), but converts to an integer(floats are truncated)
  • Rational(numerator, denominator=1) -- Creates a Rational number with the given parts
  • String(arg) -- Converts argument to string by calling to_s

此外,某些类定义 [] 作为一类方法,它是用于从基本数据类型更复杂的初始化(通常初始化只,不转换)如哈希[]

Also, some classes define [] as a class method, which is used for more complex initialization from basic data types (usual initialization only, not conversion) such as Hash[].

这篇关于哪里是Array()的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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