超载Ruby的[...]数组创建速记 [英] overloading Ruby's [...] Array creation shorthand

查看:85
本文介绍了超载Ruby的[...]数组创建速记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了几个扩展底座Ruby类与大多是通过方法走样观察了封装。不过,我已经打的阵列实例速记障碍(如 @a = [1,2,3] )我似乎无法找到这实际上是任何方法所谓由速记手段建立一个Array对象。这不是一种遗传#[] 方法在当前范围内或从祖先链中的任何类或模块继承。我还过载或从类的 #NEW 来实例的 #initialize 来的singleton_method <$看着每一个方法C $ C>#[]

I've written a library that extends several base Ruby classes with observing wrappers mostly through method aliasing. However, I've hit a roadblock with the Array instantiation shorthand (e.g. @a = [1, 2, 3] ) I can't seem to find any method that's actually called in the creation of an Array object by the shorthand means. It's not an inherited #[] method in the current scope or inherited from any class or module in the ancestors chain. I've also overloaded or watched every method from the class's #new to an instance's #initialize to the singleton_method #[] on the Array class object based on the Ruby C code

rb_define_singleton_method(rb_cArray, "[]", rb_ary_s_create, -1);

有谁知道我可以分配将是速记阵列实例实例的方法链中的方法?

Does anyone know how I can assign a method that would be within the method chain of the shorthand Array instance instantiation?

推荐答案

不幸的是,像pretty每隔很多其他节目在这个星球上语言,Ruby不允许文字超载。如果您需要字面超载,你将不得不使用它支持它的一些编程语言之一,如伊欧凯或<一个HREF =htt​​p://Seph-Lang.Org/相对=nofollow> SEPH 。

Unfortunately, like pretty much every other programming language on the planet, Ruby does not allow overloading of literals. If you require literal overloading, you will have to use one of the few programming languages which support it, like Ioke or Seph.

下面是在伊欧凯一个例子:

Here's an example in Ioke:

[] = method(foo, foo println)
[1]
; 1

和在SEPH:

[] = #(foo, foo println)
[1]
; 1

[注意这些意志,当然,肆虐与您的系统,因为,例如,伊欧凯/ SEPH标准库的很大一部分在伊欧凯/ SEPH的实施,以及他们使用列出了所有的地方,所以在生产系统中,你要正确封装这个。]

[Note that these will, of course, wreak havoc with your system, since, for example, a large part of the Ioke/Seph standard library is implemented in Ioke/Seph, and they use lists all over the place, so in a production system, you'll want to properly encapsulate this.]

这篇关于超载Ruby的[...]数组创建速记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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